拉贾帝奇 拉贾帝奇
关注数: 122 粉丝数: 1,315 发帖数: 19,707 关注贴吧数: 20
曼巴的大神们帮我看一下,这段C++程序是做什么用的呀? #include<iostream> #include <iomanip> using namespace std; class Component { private: int i; public: Component() { cout <<"调用Component的构造函数!"<<endl; } Component(int n) { i=n; cout <<"调用Component的构造函数!"<<endl; } ~Component() { cout <<"调用Component的析构函数!"<<endl; } int Get() { return i; } void Double() { i*=2; } }; class Base */基础 { private: int k; Component c1,c2; */创建c1,c2两个对像。 public: Base() {} Base(int l):k(l) { cout << "调用Base的构造函数!" <<endl; } ~Base() { cout <<"调用Base的析构函数!"<<endl; } int Get() { return k; } void Double() { k*=2; } }; class Hybird:public Base { private: int k; Component c1, c2; public: Hybird(int l):k(l) { cout << "调用Hybird的构造函数!" << endl; } Hybird(int l, int m, int n, intp):k(l), Base(m), c1(n), c2(p) { cout << "调用Hybird的构造函数!" << endl; } ~Hybird() { cout <<"调用Hybird的析构函数!"<<endl; } void Double() { k*=2; } void DoubleBase() { Base::Double(); } void DoubleComp() { c1.Double(); c2.Double(); } int GetBase() { return Base::Get(); } int Getc1() { return c1.Get(); } int Getc2() { return c2.Get(); } }; int main() { Hybird y(7,8,9,10); cout << "执行Sizeof(H)的结果是:"<< sizeof(y)/sizeof(int)<<" 个int变量!"<<endl; cout <<"y.get()=" << y.Get() << endl; cout <<"y.Getc1()=" << y.Getc1()<< endl; cout <<"y.Getc2()=" << y.Getc2()<< endl; cout << "执行y.Double()后 " << endl; y.Double(); cout <<"y.get()=" << y.Get() << endl; cout <<"y.Getc1()=" << y.Getc1()<< endl; cout <<"y.Getc2()=" << y.Getc2()<< endl; cout << "执行y.Double()后 " << endl; y.DoubleBase(); cout <<"y.get()=" << y.Get() << endl; cout <<"y.getBase()=" << y.GetBase() << endl; cout <<"y.Getc1()=" << y.Getc1()<< endl; cout <<"y.Getc2()=" << y.Getc2()<< endl; cout << "执行y.DoubleComp();后 " << endl; y.DoubleComp(); cout <<"y.get()=" << y.Get() << endl; cout <<"y.getBase()=" << y.GetBase() << endl; cout <<"y.Getc1()=" << y.Getc1()<< endl; cout <<"y.Getc2()=" << y.Getc2()<< endl; }
首页 3 4 5 6 7 8 下一页