C ++ test program 2.2.1 1
#include <iostream>
using namespace std;
class CDate
{
void Set(int m, int d, int y )
{
month = m ;
day = d ;
year = y;
}
int IsLeapYear()
{
return (year%4 == 0 && year%100 != 0)||(year%400 == 0);
}
public:
void Print()
{
cout<<month<<"/"<<day<<"/"<<year<<endl;
}
private:
int month;
int day;
int year;
};
void main()
{
CDate a; -----------------------------------------------------①
a.Set(10,15,2007);
a.Print();
}
One problem: The above program is compiled through it, why, how correct?
A: No;
The reason: because the main function can not access the private members of the class;
Correction: In the void Set (int m, int d, int y) preceded by "public:"
Second problem: CDate class in which there are member functions and member variables, which they access belong to?
answer:
Member functions
私有:void Set(int m, int d, int y )
Private: int IsLeapYear ()
Public: void Print ()
Member variables:
Private: int month; int day; int year
Question three: What ① at the statement mean?
A: create a name of a target CDate
Reproduced in: https: //www.jianshu.com/p/0560c892ecd5