Note the point:
The same member function can only be declared or defined once!!!
When the class is defined, memory does not allocate storage space for it, and the instance object is allocated.
Class declared, the system defaults to private if the private or public declaration is not made
struct declares the class, .... Public
Union declares the class, .... Public
1. Definition:
Class goods{
Public
Char nmae[10]; Defining member variables
Private
int Amount;
float price;
float Total_value;
Public
void GetName (char*); Declare member function, can not write method body,
Goods () {//constructor, cannot specify type!!! Void is not written.
cout<< "hehe";
}
void Goods::registergoods (char* name, int amount, float price) {//define member function
strcpy (Name,name);
Amount= Amount;
Price= Price;
}
void Goods::counttotal (void) {
Total_value= Price*amount;
}
float Goods::gettotal_value (void) {
return total_value;
}
float Goods::getprice (void) {
return price;
}
~goods () {//destructors, cannot specify type!!! Void is not written.
cout<< "System has default construction and destructor";
}
};
2, instance Object
Goods Cars;
3. Object Call member function
int main ()
{
Goods Car;
Char name[10];
int amount;
float price;
float total=1;
cout<< "Enter vehicle name";
Cin.getline (name,10);
cout<< "Enter quantity and price";
cin>>amount>>price;
cout<< "Total is" <<total<< "\ n";
Car.registergoods (Name,amount,price); Calling member functions
Car.counttotal (); Calling member functions
Total = Car.gettotal_value (); Calling member functions
cout<< "Vehicle name is" <<Car.Name<< "\ n"; Call member variable (public only)
cout<< "Vehicle Unit Price is" <<car.getprice () << "\ n";
}
C + + data type, class