Recent Studies on Classes and Objects
I'm good at rowing. I didn't listen to classes. It's harmful, so it's a bit cloudy.
So write this blog to understand.
The use of a class is like encapsulating something, defined as a class
The place and structure defined are actually a little similar.
It doesn't feel like much in the algorithm.
Never touched before,
Now it's interesting to see how this works.
First is the definition
class Clock{ public: void showtime(); void settime(int a,int b,int c); private: int hour,minute,second; protected: }; /* class Class name { public://External Interface public members private: Private Members protected: Protected Members }; */
Public is an external interface, a public member, that is, all parts can be called, that is, a class class class in the form of a Clock for the entire program can be called;
Private is a private member, allowing only internal members to use and mobilize, not external ones.
Simply put, if Clock c is defined, c. Public members () are legal and c. private members () are invalid.
Implementation of member function
void Clock::showtime(){ cout<<hour<<":"<<minute<<":"<<second<<endl; } /* Return value class name:Function member name (parameter table) { Function Body }
*/
Inline member function
There are two ways to declare
- Implicit declaration
-
class Clock{ public: void showtime(); void settime(int a,int b,int c){ hour=a; minute=b; second=c; } private: int hour,minute,second; protected: };
The settime function is an implicit declaration
- Explicit declaration
-
class Clock{ public: void showtime(); void settime(int a,int b,int c); private: int hour,minute,second; protected: }; inline void Clock::settime(int a,int b,int c){//Explicit hour=a,minute=b,second=c; }
Constructor
Constructor is generated by itself at the beginning of class creation, default
clock is another example
class Clock{ public: Clock(int a,int b,int c);//Constructor Clock(){//Constructor hour=0;minute=0;second=0; } void showtime(); void settime(int a,int b,int c); private: int hour,minute,second; }; Clock::Clock(int a,int b,int c){ hour=a,minute=b,second=c; } //Other member functions omitted int main(){ Clock c1(1,1,1);//A parameterized constructor was called Clock c2; //A parameterless constructor was called //Other slightly }
ps: Clock c2 will fail if there is no parameterless constructor in the class;
Copy Constructor/Copy Constructor
Class class class name { public: Class name (formal parameter table);//constructor Class name (class name & object name); //copy constructor ...... }; Class Name: Class Name (Class Name &Object Name) {//Implementation of Copy Constructor Function Body }
There are three ways to call a copy constructor:
(Not combed yet, leave first)
Destructor
Destructors and constructors exist directly by default
When the destructor is cleaned up, it completes the cleaning up before the object is deleted.
class Clock{ ..... ~Clock();//Destructor };
- Destructors do not accept any parameters
- The ~clock destructor can be used when it needs to be cleaned up earlier (it feels like a return break)
Use as much of the above knowledge as possible in a single person
Example 4-3
A circular aisle needs to be built around a swimming pool
Fence the periphery of a circular aisle
The fence price is 35 yuan/m
The construction price of the aisle is 20 yuan per square meter
The width of the aisle is 3 meters
Pool radius entered by keyboard
/* Example 4-3 A circular aisle needs to be built around a swimming pool Fence the periphery of a circular aisle The fence price is 35 yuan/m The construction price of the aisle is 20 yuan per square meter The width of the aisle is 3 meters Pool radius entered by keyboard */ #include<cstdio> #include<cstring> #include<iostream> #include<cmath> #include<algorithm> #include<queue> #include<utility> #include<stack> #include<cstdlib> #define ll long long #define inf 0x3fffffff #define pi 3.1415926535 #define concrete_price 20 #define fence_price 35 #define length 3 using namespace std; class price{ //Class definition is used to resolve expenses private: //Private data member (for internal calls only) double cir,area; public: //External Interface or public members price(double c,double a){ //Inline constructor implicit declaration cir=c,area=a; } double buy_concrete(); double buy_fence(){ //Implicit declaration of inline member functions return area*concrete_price; } }; inline double price::buy_concrete(){ //Explicit declaration of inline member functions return cir*fence_price; } class circle{ //The definition of the class is used to solve the area and perimeter private: double radius; double real_area(){ //Private member function calculates actual area double r=radius; double R=radius+3; double s1=pi*r*r; double s2=pi*R*R; return s2-s1; } public: circle(double r); //Constructor circle(){ //Constructor initializes the value of a private member radius=0; } circle(circle &C); //copy constructor or copy constructor double make_circumference(); double make_area(); ~circle(){}; //Destructor (followed by {} Not less) to delete data and clean up }; circle::circle(double r){ //Implementation of Constructor radius=r; } circle::circle(circle &C){ //Implementation of copy constructor radius=C.radius; } double circle::make_circumference(){ //Implementation of member function return 2*pi*(radius+3); } double circle::make_area(){ //Implementation of member function return real_area(); } int main(){ int r; cin>>r; circle Circle(r); //Value of constructor initialization radius circle c1=Circle; //Call of copy constructor to initialize value circle c2; //Constructor initialized directly to 0 double c_a=c1.make_area(); double c_c=c1.make_circumference(); cout<<c_c*fence_price<<endl<<c_a*concrete_price; price ans(c_c,c_a); //Value of initialization constructor cout<<"Fence Price:"<<ans.buy_fence()<<endl; cout<<"Passage Price:"<<ans.buy_concrete()<<endl; return 0; }
Next you need to learn the combinations of classes
Preview first, harm