C + + data type, class

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.