C + + 's object-oriented programming idea is as follows, the general case is that a class contains all the properties and functions of the object, the object can be called directly to perform any operation it can use.
#include <iostream>class Robot{public:robot (): Battery (100) {}//Constructs a robot with a full grid void speak ();//Speech function void Charge ();//Charge function void functions (int i);//select function function private:unsigned short battery;//battery};int main () {Robot Shaqiang; Shaqiang.speak (); System ("pause"); return 0;} void robot::function (int i) {switch (i) {case 1:if (Battery <) {Std::cout << "insufficient power to execute \ n"; break;} Else{battery-= 10;std::cout << "The tea has been brewed and the boss please drink!" \ n "; break;} Case 2:if (Battery <) {Std::cout << "insufficient power to execute \ n"; break;} Else{battery-= 15;std::cout << "Coffee has been brew boss please drink!" \ n "; break;} Case 3:if (Battery > 5) {std::cout << "standby succeeded \ n"; break;} Else{std::cout << "standby succeeded \ n"; std::cout << "Hint: power is insufficient 5%\n"; Default:std::cout << "input error \ n"; Break;}} void Robot::speak () {int i = 0;char x = ' 0 '; while (1) {if (Battery < 1) {Std::cout << "power is 0 forced charge ... \ n"); Battery + = 30;std::cout << "Force charging complete Current power:" << Battery << Std::endl;} if (Battery <=) {std::cout << current charge: << Battery ≪< "need to recharge?" Y/n\n "; std::cin >> x;if (x = = ' Y ' | | x = = ' Y ') {charge ();} else if (x = = ' n ' | | x = = ' n ') {std::cout << "do not charge \ n";} else std::cout << "input error \ n";} Battery-= 1;std::cout << "The boss is drinking tea or coffee \"; std::cout << "(1) Tea (2) coffee (3) Standby:"; std::cin >> i;function (i) if (i = = 3) {break;}}} void Robot::charge () {std::cout << "current charge:" << this->battery << std::endl;if (Battery = =) {std:: cout << "Power is full no need to charge \ n";} Else{std::cout << "Charging process won't write! Temporarily omitted! \ n "; this->battery = 100;std::cout <<" charge complete current charge is: "<< this->battery << Std::endl;}}
C + + Object-oriented programming thought robot