Class: in C + + broadly divided into two, a built-in type, such as int double user two user-defined class UDT
Q: Why do I use a user-defined class?
A: The compiler cannot know all the types we want to use. But the standard library type has provided us with some very useful classes, such as Vector,string,ostream and so on, but far from satisfying the requirements. Especially in object-oriented program design, we need to use a custom type to represent some of the characteristics of this object, so in object-oriented programs we see more of a single object, and in C programs we see more functions and expressions. Class is also the key and basis for describing an object.
1. The expression of the class:
Class x{
Public
int m;
int MF (int v) {int odd = M;m = V;return Odd}
};
Used to define the components of a class, which are called members . The read member rule is as follows:
X var;
VAR.M = 7;
VAR.MF (9);
2. Interfaces and implementations
Class x{
Public
This is the identity of the interface, it is public
Private
This is the implementation ID, it's private.
}
The member is private by default, that is, if it is not identified, it is equivalent to a private
Http://soft.chinabyte.com/database/340/12457340.shtml Classic What is invariant
C + + provides a class that can be used to describe details without a private implementation, that is, a struct
struct x{
int m;
} ; Equivalent to
Class x{
Public
int m;
};
2. member functions and constructors
struct date{struct Date{
int y; int y,m,d;
int m &N Bsp , &NB Sp date (int y,int m,int D);//a member with the same name as a class is a special member, called constructor,
specifically for initialization. The type itself has a constructor that requires parameters, and if the programmer forgets to use it, the system The system will error!!!
int D; void Add_day (int n);
} ; };
For example:
Date Mybirthday;//error:mybirthday Not initial
Date today; Date today (12,24,2007);//error date is illegal
TODAY.Y = 2005; Date last (2000,12,31);//right
TODAY.M = 24;//error illegal month Date Christams = Date (1976,12, //ok Trouble
TODAY.D = -3;//Illegal date We need auxiliary functions for initialization checks, data legitimacy checks
2. Maintain the details of private
The above structure, the user can arbitrarily modify the structure of the data, such as Date last (2000,12,31);
LAST.M = 14;//illegal, unreasonable month
To avoid this big mistake, we make the data private and cannot be modified by the user, but provide a public function that is only accessible to the user.
Class date{
Private
int y,m,d;
Public
Date (int y,int m, int d);
void Add_day (int n);
int year () {return y};
Int month () {return m};
int day () {return d};
};
Date last (2000,12,31);
count<< last.month (); <<endl;//ok
The rule of determining valid worth is called invariant
If we do not need to deal with the data, the provisions of the invariant, then we are dealing with ordinary data, then use a struct, do not use the class
Structs cannot protect data in their own structure, all data is public and can be modified arbitrarily
C + + Basic Learning about classes