




已阅读5页,还剩12页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
/*定义一个Dog类,包含age,weight等属性,以及对这些属性操作的方法。实现并测试这个类*/#includeusing namespace std;class Dogpublic:Dog(int a,int b)age=a;weight=b;Dog()int getAge()return age;int getWeight()return weight;void setAge(int age)this-age=age;void setWeight(int weight)this-weight=weight;private:int age,weight;int main()Dog jack(2,10);coutjack is a Dog who is ;coutjack.getAge() years old and jack.getWeight() pounds weightendl;jack.setAge(7);jack.setWeight(20);coutNow jack is ;coutjack.getAge() years old and jack.getWeight() pounds weightendl;return 0;/*定义一个Circle类,有数据成员radius(半径)成员函数getArea(),计算圆的面积,构造一个Circle的对象进行测试。*/#includeusing namespace std;class Circlepublic:Circle(float radius)this-radius=radius;Circle()float getArea()return 3.14*radius*radius;private:float radius;int main()float radius;coutradius;Circle p(radius);cout半径为radius的圆的面积为:p.getArea()endl;return 0;/*定义一个Tree(树)类,有成员ages(树龄),成员函数grow(int years)对ages加上years,age()显示tree对象的ages值。*/#includeusing namespace std;class Treepublic:Tree(int ages=0)this-ages=ages;Tree()show();void grow(int years)ages+=years;void show()cout树的年龄为:agesendl;private:int ages;int main()Tree e1(10);e1.show();e1.grow(5);return 0;/*定义一个复数类Complex,使得下面的代码能够工作Complex c1(3,5); /用复数3+5i初始化c1Complex c2=4.5; /用实数4.5初始化c2c1.add(c2); /将c1与c2想加,结果保存在c1中c1.show(); /将c1输出(这时的结果应该是7.5+5i)*/#includeusing namespace std;class Complexpublic: Complex(double r,double i) real=r;image=i; Complex(double r) real=r;image=0; void show() coutreal+; coutimagei; coutendl; void add(Complex c2)real+=c2.real;image+=c2.image;private: double real;double image;int main() Complex c1(3,5); Complex c2(4.5); c1.show(); c1.add(c2); c1.show(); return 0;/*定义一个Cat类,拥有静态数据成员numOfCats记录Cat的个体数目,静态成员函数getNumOfCats()存取numOfCats,设计程序测试这个类,体会静态数据成员和静态成员函数的用法。*/#includeusing namespace std;class Catpublic:Cat(int age)itsAge=age;numOfCats+;virtual Cat()numOfCats-;virtual int getAge()return itsAge; virtual void setAge(int age)itsAge=age; static int getNumOfCats()return numOfCats;private:int itsAge;static int numOfCats;int Cat:numOfCats=0;void telepathicFunction()coutThere are Cat:getNumOfCats()cats alive !n;int main()const int maxCats=5;Cat*catHousemaxCats;int i;for(i=0;imaxCats;i+)catHousei=new Cat(i);telepathicFunction();for(i=0;imaxCats;i+)delete catHousei;telepathicFunction();return 0;/*在函数fn1()中定义一个静态变量nfn1()中对n的值加1,在主函数中,调用fn1()10次,显示n的值。*/#includeusing namespace std;void fn1()static int n=0;n+;coutn的值为:nendl;int main()int i;for(i=0;i10;i+)fn1();return 0;/*实现一个名为 SimpleCircle的简单圆类其数据成员int *itsRadius为一个指向其半径的指针设计对数据成员的各种操作,给出这个类完整的实现并且测试这个类*/#includeusing namespace std;class SimpleCirclepublic:SimpleCircle()itsRadius=new int(5);SimpleCircle(int radius)itsRadius=new int(radius);SimpleCircle(const SimpleCircle & rhs)int val=rhs.getRadius();itsRadius=new int(val);SimpleCircle()int getRadius()constreturn *itsRadius;private:int *itsRadius;int main()SimpleCircle CircleOne,CircleTwo(9);coutCircleOne:CircleOne.getRadius()endl;coutCircleTwo:CircleTwo.getRadius()endl;return 0;/*定义一个Employee类,其中包括鞭尸姓名、地址城市、和邮编等属性,包括setName()和display()等函数。display()使用cout语句显示姓名、地址、城市和邮编等属性。函数setName()改变对象的姓名属性,实现并测试这个类。*/#include#includeusing namespace std;class Employeeprivate:char name30;char street30;char city15;char zip15;public:Employee(char* n,char* s,char* c,char* z)strcpy(name,n);strcpy(street,s);strcpy(city,c);strcpy(zip,z);void setName(char* n)strcpy(name,n);void display()coutnametstreett;coutcitytzipendl;int main()Employee e1(张三,平安大街,北京,100000);e1.display();e1.setName(李四);e1.display();coutendl;return 0;/*定义一个Shape,在此基础上派生出Rectangle和Circle,二者都有getArea()函数计算对象的面积。使用Rectangle类创建一个派生类Square。*/#includeusing namespace std;class Shapepublic:Shape()Shape()virtual float getArea()return -1;class Circle:public Shapepublic:Circle(float radius):itsRadius(radius)Circle()float getArea()return 3.14*itsRadius*itsRadius;private:float itsRadius;class Rectangle:public Shapepublic:Rectangle(float len,float width):itsLength(len),itsWidth(width);Rectangle();virtual float getArea()return itsLength*itsWidth;virtual float getLength()return itsLength;virtual float getWidth()return itsWidth;private:float itsLength;float itsWidth;class Square:public Rectanglepublic:Square(float len);Square();Square:Square(float len):Rectangle(len,len)int main()Shape *sp;sp=new Circle(5);coutThe area of the Circle isgetArea()endl;delete sp;sp=new Rectangle(4,6);coutThe area of the Rctangle isgetArea()endl;delete sp;sp=new Square(5);coutThe area of the Square isgetArea()endl;delete sp;return 0;/*定义一个哺乳动物类Mammal,再由此派生出Dog类,定义一个Dog类的对象,观察与派生类的构造函数的调用顺序Manmal constructor.Dog constructor.Mammal sound!Tail wagging.jack is 1 year oldDog destructor.Mammal destructor.*/#includeusing namespace std;enum myColorBLACK,WHITE;class Mammalpublic:/constructorsMammal();Mammal(); /accesasorsint getArea()constreturn itsAge;void setAge(int age)itsAge=age;int getWeight()constreturn itsWeight;void setWeight(int weight)itsWeight=weight;/Other menthodsvoid speak()constcoutMammal sound!n;protected:int itsAge;int itsWeight;class Dog:public Mammalpublic:Dog(); Dog();myColor getColor() constreturn itsColor; void setColor(myColor color)itsColor=color; void wagTail()coutTail wagging.n; private:myColor itsColor;Mammal:Mammal():itsAge(1),itsWeight(5)coutMammal constructor.n;Mammal:Mammal()coutMammal destructor.n;Dog:Dog():itsColor(WHITE)coutDog constructor.n;Dog:Dog()coutDog destructor.n;int main()Dog jack;jack.speak();jack.wagTail();coutjack isjack.getArea()years oldn;return 0;/*编写一个哺乳动物类Mammal,再由此派生出狗类Dog,二者都声明speak()成员函数,该函数在基类中被声明为虚函数,声明一个Dog类的对象,通过此对象调用speak函数,观察运行结果。*/#includeusing namespace std;class Mammalpublic:Mammal()coutMammal constructor.n;virtualMammal()coutMammal destructor.n;virtual void speak()constcoutMammal speak!n;class Dog:public Mammalpublic:Dog()coutDog Constructor.n;Dog()coutDog destructor.n;void speak()constcoutspeak();delete pDog;return 0;/*请编写一个抽象类Shape,在此基础上派生出类Rectangle和Circle,二者都有计算对象面积的函数getArea(),计算对象周长的函数getPerim()。*/#includeusing namespace std;class Shapepublic:Shape()Shape()virtual float getArea()=0;virtual float getPerim()=0;class Circle:public Shapepublic:Circle(float radius):itsRadius(radius);Circle()float getArea()return 3.14*itsRadius*itsRadius;float getPerim()return 6.28*itsRadius;private:float itsRadius;class Rectangle:public Shapepublic:Rectangle(float len,float width):itsLength(len),itsWidth(width);Rectangle();virtual float getArea()return itsLength*itsWidth;float getPerim()return 2*itsLength+2*itsWidth;virtual float GetLength()return itsLength;virtual float GetWidth()return itsWidth;private:float itsLength;float itsWidth;int main()Shape *sp;sp=new Circle(5);coutThe area of the Circle isgetArea()endl;coutThe Perimeter of the Circle isgetPerim()endl;delete sp;sp=new Rectangle(4,6);coutThe area of the Rectangle isgetArea()endl;coutThe perimter of the Rectangle isgetPerim()endl;delete sp;return 0;/*对类Point重载+(自增)、-(自减)运算符,要求同时重载前缀和后缀的形式。*/#includeusing namespace std;class Pointpublic:Point& operator+();Point operator+(int);Point& operator-();Point operator-(int);Point()_x=_y=0;int x()return _x;int y()return _y;private:int _x,_y;Point&Point:operator+()_x+;_y+;return * this;Point Point:operator+(int)Point temp=* this;+ * this;return temp;Point&Point:operator-()_x-;_y-;return *this;Point Point:operator-(int)Point temp=*this;-*this;return temp;i
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
评论
0/150
提交评论