1、
#include <iostream>using namespace std;class Rec{int length;int width;public:void set_length(int l);void set_width(int h);int get_length();int get_width();void show();};void Rec::set_length(int l){length=l;}void Rec::set_width(int h){width=h;}int Rec::get_length(){return length;}int Rec::get_width(){return width;}void Rec::show(){cout << "周长=" << (length+width)*2 << endl;}int main(){Rec p1;p1.set_length(10);p1.set_width(8);p1.show();return 0;}
2、
#include <iostream>using namespace std;class j{double r;public:double PI=3.14;void set_r(double re);double get_r();void show();};void j::set_r(double re){r=re;}double j::get_r(){return r;}void j::show(){cout << "周长=" << 2*PI*r << endl;cout << "面积=" << PI*r*r << endl;}int main(){j p1;p1.set_r(4);p1.show();cout << "Hello World!" << endl;return 0;}
3、
#include <iostream>using namespace std;class car{string brand;string color;int speed;public:void display(string brand_a,string color_a,int speed_a);void show();};void car::display(string brand_a, string color_a, int speed_a){brand=brand_a;color=color_a;speed=speed_a;}void car::show(){cout << "brand=" << brand <<endl;cout << "color=" << color << endl;cout << "speed=" << speed << endl;}int main(){car p1;p1.display("荣耀汽车","黑外内棕",50);p1.show();cout << "Hello World!" << endl;return 0;}
思维导图