C + + Object-oriented programming thought robot

Source: Internet
Author: User

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 &Lt;< "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

Related Article

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.