代码:
#include<iostream> using namespace std; class Table { public: Table(){row = 10; col = 10;} Table(int a, int b):row(a), col(b){}; ~Table(){}; void show(); private: int row; int col; }; void Table::show() { cout << "row:" << row << endl; cout << "col:" << col << endl; } int main() { Table t1; Table t2(10, 10); t1.show(); t2.show(); return 0; }
结果:
代码:
#include <iostream> using namespace std; class SDate { private: int year; int month; int day; public: SDate(int y,int m,int d); void outShow() { cout<<"year:"<<year<<endl; cout<<"month:"<<month<<endl; cout<<"day:"<<day<<endl; } ~SDate(){cout<<year<<month<<day<<endl;} }; SDate::SDate(int d,int m=4,int y=2021) { year=y; month=m; day=d; } int main() { SDate s1(20); s1.outShow(); SDate s2(5,20); s2.outShow(); return 0; }
结果:
代码:
#include<iostream> using namespace std; class SimpleClass { public: SimpleClass(char *time) { cout << "Create a SimpleClass!" << time << "\n"; } }; SimpleClass s("before main"); int main () { SimpleClass sm("in main"); return 0; } SimpleClass sa("after main");
结果:
代码:
#include<iostream> using namespace std; class whatYouLike { public: whatYouLike(char *m) { strcpy(fruit, m); fruit[19] = '\0'; cout << "I like "<< fruit << "\n"; } char fruit[20]; }; void fn(char *f) { static whatYouLike s(f); static int i; i++; cout << i << " in fn,give you a(/an) " << f << "\n"; cout << "WhatYouLike is" << s.fruit << endl; } int main() { fn("apple"); fn("orange"); fn("banana"); return 0; }
结果:
代码:
#include<iostream> using namespace std; class SClass { public: SClass() { cout << "Create a SClass object!" << "\n"; } }; int main() { SClass *sq; sq = new SClass(); cout << "Main creates a SClass object in heap!\n"; delete sq; }
结果:
代码:
#define N 20 #include<iostream> #include<string> using namespace std; class cupCover { public: cupCover(char *myColor) { strcpy(color, myColor); color[N-1] = '\0'; cout << "The cupCover is create! color=" << color << endl; } private: char color[N]; }; class cupBody { public: cupBody() { cout << "The cupBody is created!\n"; } }; class cup { public: cup(char *colors):cupCover(colors) { cout << "The cup is created!It has cupCover and cupBody!\n"; } private: cupCover cupCover; cupBody cupBody; }; int main() { cup littleCup("black"); return 0; }
结果:
代码:
#include<iostream> #include<string> using namespace std; class Employee { private: int number; string name; string department; public: Employee(int num,string n,string d) { number=num;name=n;department=d; } void change1(int c1,string c2,string c3) { change2(c1,c2,c3); } ~Employee() { cout<<"修改成功"<<endl; } void print() { cout<<"number:"<<number<<" name:"<<name<<" department:"<<department<<endl; } protected: void change2(int c1,string c2,string c3) { number=c1,name=c2,department=c3; } }; int main() { int a,i; string b,c,j,k; cout<<"please input the employee's number,name and department:"; cin>>a>>b>>c; Employee a1(a,b,c); cout<<"当前员工信息为:"<<endl; a1.print(); cout<<"需要修改,请输入你要修改的编号,性名,部门:"; cin>>i>>j>>k; a1.change1(i,j,k); a1.print(); return 0; }
结果:
代码:
#include <iostream> using namespace std; class MyDesk { private: int long0; int wide; public: MyDesk() { long0=3,wide=2; } MyDesk(int x):long0(x) { wide=3; } MyDesk(int x,int y):long0(x),wide(y) { } void print() { cout<<long0<<" and "<<wide<<endl; } }; int main() { MyDesk my1; cout<<"无参数的构造函数的长宽为"<<endl; my1.print(); MyDesk my2(3); cout<<"一个参数的构造函数的长宽为"<<endl; my2.print(); MyDesk my3(4,5); cout<<"两个参数的构造函数的长宽为"<<endl; my3.print(); return 0; }
结果: