Once you have defined a class, you can create an object of that class just as you would declare a simple variable with a type of int, double, and so on, calledinstantiation of a class。 The definition of a class is actually defined as a type that the class does not receive or store specific values, only as a "blueprint" for generating concrete objects.The system allocates storage space for objects only after the class is instantiated and the object is created (a variable that declares the class).
Computer.hclass Computer//class definition { private: char brand[20]; float price; Public: void print (); void Setbrand (const char * sz); void Setprice (float PR); };
#include "computer.h"//contains class definition #include <iostream>using namespace std; void computer: Implementation of the:p rint ()//member function { cout << branding: << brand << Endl; cout << "Prices:" << price << Endl; } void Computer::setbrand (char * sz) { strcpy (brand, SZ);//String copy} void Computer::setprice (float PR) { price = PR; } #include "Computer.h"//defines class computer int main ()//main function { computer COM1;//Declares Computer class object (or class variable) COM1 com1 . Setbrand ("Lenovo");//Call the public member function Setbrand set branding brand com1. Setprice (8000); Call public member function Setprice set brand price com1.print ();//Information output return 0;}
Use of the 7--c++ class