c++实验多态程序设计
- 运行程序,分析结果。
#include <iostream>
using namespace std;
class B
{
public:
????virtual void f1(double x)
????{
????????cout<<"B::f1(double)"<<x<<endl;
????}
????void f2(double x)
????{
????????cout<<"B::f2(double)"<<2*x<<endl;
????}
????void f3(double x)
????{
????????cout<<"B::f3(double)"<<3*x<<endl;
????}
};
class D:public B
{
public:
????virtual void f1(double x)
????{
????????cout<<"D::f1(double)"<<x<<endl;
????}
????void f2(double x)
????{
????????cout<<"D::f2(double)"<<2*x<<endl;
????}
????void f3(double x)
????{
????????cout<<"D::f3(double)"<<3*x<<endl;
????}
};
int main()
{ ??D d;
????B* pb=&d;
????D* pd=&d;
????pb->f1(1.23);
????pd->f1(1.23);
????pb->f2(2.23);
????pd->f2(2.23);
????pb->f3(3.23);
????pd->f3(3.23);
????return 0;
}
运行结果pb与pd一样,基类f1为虚函数,派生类D为虚函数并重写了f1
因此调用派生类版本
运行结果与传入实参相关,若修改pd结果运行结果就改变
改变类D的函数体结果与形参和函数体定义相关,类D函数f1()为虚函数对象pb为类D的实例所以改变类D的函数体后对象pb的f1()的运行结果,虚函数是根据对象的不同调用不同类的虚函数,pb是动态联编,pb的f1结果为pb的f1结果
?
运行下面的程序,分析运行结果;再把基类的析构函数定义为virtual,再运行程序,分析运行结果,体会将基类的析构函数定义为虚析构函数的作用。
#include <iostream>
using namespace std;
class BaseClass
{
public:
????BaseClass(){cout<<"BaseClass()"<<endl;}
?????~BaseClass() { cout<<"~BaseClass()"<<endl;}
};
class DerivedClass:public BaseClass
{
public:
????DerivedClass(){cout<<"DerivedClass()"<<endl;}
????~DerivedClass(){ cout<<"~DerivedClass()"<<endl;}
};
int main()
{
????BaseClass *bp=new DerivedClass();
????delete bp;
????return 0;
}
最先调用基类的构造函数,其次调用派生类的构造函数,调用基类的构造函数,最后只是调用了基类的构造函数
基类的析构函数声明为虚构函数以后调用派生类的构造函数,调用基类的构造函数,调用派生类的构造函数,调用基类的构造函数
编写程序,计算三角形、正方形和圆形这3种图形的周长和面积,并在main函数中使用相关数据进行测试。要求:
(1)抽象出一个基类Shape类,从其派生出Triangle类(三角形类)、Square(正方形类)、Circle(圆形类)。
Shape类中定义3个protected数据成员供派生类使用,派生类中无需定义数据成员。
Shape类中定义求面积getArea()和求周长getcircumference()成员函数为虚函数。其派生类中分别重定义各自的求面积和求周长函数。
#include<iostream>
using namespace std;
#define PI 3.14
class Shape
{
public:
Shape(){}
~Shape() {};
virtual double getArea() const
{
return 0.0;
}
virtual double getcircumference()const
{
return 0.0;
}
protected:
double x;
double y;
double z;
private:
};
class Triangle:public Shape
{
public:
Triangle()
{
}
Triangle(double X, double Y, double Z) {
x = X;
y = Y;
z = Z;
};
~Triangle() {};
virtual double getcircumference() const
{
return (x + y + z);
}
virtual double getArea() const
{
double a= (x + y + z)/2;//海伦公式
double s= (a*(a-x)*(a-y)*(a-z));
return s;
}
private:
};
class Square: public Shape
{
public:
Square() {};
Square(double X) {
x = X;
};
~Square() {};
virtual double getArea() const
{
return x * x;
}
virtual double getcircumference()const
{
return 4 * x;
}
private:
};
class Circle :public Shape
{
public:
Circle() {};
~Circle() {};
Circle(double X) {
x = X;
};
virtual double getArea() const
{
return PI * x * x;
}
virtual double getcircumference()const
{
return PI * 2 * x;
}
private:
};
void fun(Shape &s)
{
cout << s.getcircumference()<<" ";
cout << s.getArea()<<endl;
}
int main()
{
Triangle t(4,2,1);
cout << "Triangle的周长和面积分别为:";
fun(t);
Square s(5);
cout << "Square的周长和面积分别为:";
fun(s);
Circle c(3);
cout << "Square的周长和面积分别为:";
fun(c);
}
- 一个游戏中有多种怪物(Monster),怪物之间可能要发生战斗(fight),每场战斗都是一个怪物与另一怪物之间的一对一战斗。每个怪物都有自己的生命值(hitpoint)、攻击力值(damage)和防御力值(defense),每种怪物都有各自特有的攻击(attack)方式,产生相应的攻击效果;战斗时,两个怪物依次攻击对方,即怪物a首先攻击怪物b, 然后轮到怪物b攻击怪物a, 之后,怪物a再次攻击怪物b,…, 直到一方生命值为0。请根据你对上述描述的理解:(1)定义并实现怪物类Monster,成员的设计可以包括数据成员hitpoint、damage和defense,以及其他任意需要的成员函数。要求包括一个纯虚函数attack,作为派生类对象各自实现攻击细节的接口;要求定义一个非虚的成员函数fight,用来描述与另外一个怪物进行战斗的过程,该函数的实现可为Monster类的任意派生类所复用(派生类不需重新定义及实现)。不必考虑怪物的生命值减少至0后如何处理。
-
猫进攻导致对方的生命值减少量:
狗进攻导致对方的生命值减少量:
(猫的攻击力值 * 2 — 对方的防御力值)
?若上式小于1,则取1
(狗的攻击力值 — 对方的防御力值 +5 )*2
若上式小于2,则取2
- 作为怪物的特例,猫和狗的攻击效果如下表所示。在Monster的基础上,以继承手段定义并实现这两个类。
-
(3)再增加其他派生类,如Cock类,自行定义进攻对方的生命值减少量。
?自行设计并实现游戏的测试函数,输出相互攻击的实时生命力值,并给出胜负结果
#include <iostream> using namespace std; class Monster { public: Monster() { } Monster(int hp, int dmg, int def) : hitpoint(hp), damage(dmg), defense(def) {} ~Monster() { } virtual void attack(Monster& m) const = 0; void fight(Monster& m) { cout << "战斗开始!" << endl; while (m.hitpoint>0&&hitpoint>0) { attack(m); if (m.hitpoint > 0) { m.attack(*this); } } cout << "战斗结束" << endl; } int hitpoint; //血量 int damage; //攻击 int defense; //防御 }; class Monster1:public Monster { public: Monster1() { } Monster1(int hp, int dmg, int def) : Monster(hp, dmg, def) {} ~Monster1() { } void attack(Monster& m)const { cout << "Monster1 使用了恶龙咆哮!"<<endl; int dam = damage - m.defense; m.hitpoint -= dam; if (m.hitpoint <= 0) { cout << "Monster1赢了" << endl; m.hitpoint = 0; } cout << "造成了:" << dam << "点血量"<<endl; cout << "对方剩余" << m.hitpoint<<"点血量" <<endl; } private: }; class Monster2:public Monster { public: Monster2() { } Monster2(int hp, int dmg, int def) : Monster(hp, dmg, def) {} ~Monster2() { } void attack(Monster& m) { cout << "Monster2 使用了群魔乱舞!" << endl; int dam = damage - m.defense; m.hitpoint -= dam; if (m.hitpoint <= 0) { cout << "Monster2赢了" << endl; m.hitpoint = 0; } cout << "造成了:" << dam << "点血量" << endl; cout << "对方 剩余" << m.hitpoint << "点血量" << endl; } private: }; class CAT :public Monster { public: CAT() { } CAT(int hp, int dmg, int def) : Monster(hp, dmg, def) {} ~CAT() { } void attack(Monster& m)const { cout << "CAT 使用了猫血旺!" << endl; int dam = (damage*2) - m.defense; cout << dam << endl; if (dam < 1)dam = 1; m.hitpoint -= dam; if (m.hitpoint <= 0) { cout << "CAT" << endl; m.hitpoint = 0; } cout << "造成了:" << dam << "点血量" << endl; cout << "对方 剩余" << m.hitpoint << "点血量" << endl; } private: }; class DOG :public Monster { public: DOG() { } DOG(int hp, int dmg, int def) : Monster(hp, dmg, def) {} ~DOG() { } void attack(Monster& m)const { cout << "DOG 使用了狗急了跳墙!" << endl; int dam =( damage - m.defense+5)*2; if (dam < 2)dam = 2; m.hitpoint -= dam; if (m.hitpoint <= 0) { m.hitpoint = 0; cout << "造成了:" << dam << "点血量" << endl; cout << "对方 剩余" << m.hitpoint << "点血量" << endl; cout << "DOG赢了" << endl; return; } cout << "造成了:" << dam << "点血量" << endl; cout << "对方 剩余" << m.hitpoint << "点血量" << endl; } private: }; class Cock:public Monster { public: Cock() { } Cock(int hp, int dmg, int def) : Monster(hp, dmg, def) {} ~Cock() { } void attack(Monster& m)const { cout << "Cock 使用了公鸡独立!" << endl; int dam = (damage - m.defense + 50) ; if (dam < 2)dam = 2; m.hitpoint -= dam; if (m.hitpoint <= 0) { m.hitpoint = 0; cout << "造成了:" << dam << "点血量" << endl; cout << "对方 剩余" << m.hitpoint << "点血量" << endl; cout << "Cock赢了" << endl; return; } cout << "造成了:" << dam << "点血量" << endl; cout << "对方剩余" << m.hitpoint << "点血量" << endl; } private: }; int main() { //Monster1 m1(100, 30, 10); //Monster1 m2(100, 25, 8); //m1.fight(m2); CAT c(50, 30, 10); //DOG d(90, 50, 40); Cock k(50, 50, 50); c.fight(k); }
设计一个复数Complex类,实现复数的加、减运算,并实现复数的输入、输出。已知测试main函数如下:
int main( )
{
????Complex c1(5.0,10.0); ?? //5+10i
????Complex c2(3.0,-2.5); ??? //3-2.5i
????Complex c3,c4,c5=20;
????c3=c1+c2;
????c4=c1-5;
????cout<<c1<<c2<<c3<<c4;
????Complex c5;
????cout<<"请输入一个复数,实部和虚部以空格分开:"<<endl;
????cin>>c5;
????cout<<"输入的复数为:"<<c5<<endl;
????c4=5+c5;
????cout<<c4<<endl;
????return 0;
}
#include <iostream> using namespace std; class Complex { private: double real; // 实部 double imag; // 虚部 public: // 构造函数 Complex(double r = 0.0, double i = 0.0) : real(r), imag(i) {} // 加法运算 Complex operator+(const Complex& other) const { return Complex(real + other.real, imag + other.imag); } // 实数 + 复数 friend Complex operator+(double realPart, const Complex& c) { return Complex(realPart + c.real, c.imag); } // 实数 - 复数 friend Complex operator-(double realPart, const Complex& c) { return Complex(realPart - c.real, -c.imag); } // 减法运算 Complex operator-(const Complex& other) const { return Complex(real - other.real, imag - other.imag); } // 输出复数 friend std::ostream& operator<<(std::ostream&os, Complex& c) { os << c.real; if (c.imag >= 0)os << '+'; os << c.imag <<"i" << endl; return os; } // 输入复数 friend std::istream& operator>>(std::istream& is, Complex& c) { std::cout << "输入实部: "; is >> c.real; std::cout << "输入虚部: "; is >> c.imag; return is; } }; int main() { Complex c1(5.0, 10.0); //5+10i Complex c2(3.0, -2.5); //3-2.5i Complex c3, c4, c5 = 20; c3 = c1 + c2; c4 = c1 - 5; cout << c1 << c2 << c3 << c4; Complex c6; cout << "请输入一个复数,实部和虚部以空格分开:" << endl; cin >> c6; cout << "输入的复数为:" << c5 << endl; c4 = 5 + c5; cout << c4 << endl; return 0; }
设计一个三角形类Triangle,包含三角形三条边长的私有数据成员,另有一个重载运算符“+",以实现求两个三角形对象的面积之和,进一步实现3个或更多的三角形对象相加的面积之和。
分析提示:在Triangle类中设计一个友元函数double operator+(Triangle t1,Triangle t2),它重载运算符“+",返回t1和t2两个三角形的面积之和。
已知测试main函数如下:
int main()
?{
?????Triangle tri1(3,4,5),tri2(6,8,10);
?????double area1=tri1+tri2;
?????cout<<area1<<endl;
?????Triangle tri3(9,12,15);
?????double area2=tri1+tri2+tri3;
?????cout<<area2<<endl;
?????return 0;
?}
#include <iostream> using namespace std; class Triangle { public: Triangle(double a=0, double b=0,double c=0) { x = a; y = b; z = c; } ~Triangle() { } friend double operator + (const Triangle& other1,const Triangle& other2) { double a1 = (other1.x + other1.y + other1.z) / 2; double a2 = (other2.x + other2.y + other2.z) / 2; double s1 = sqrt(a1 * (a1 - other1.y) * (a1 - other1.x) * (a1 - other1.z)); double s2 = sqrt(a2 * (a2 - other2.x) * (a2 - other2.y) * (a2 - other2.z)); return s1 + s2; } friend double operator + (double snum,const Triangle& other) { double a = (other.x + other.y + other.z) / 2; double s = sqrt(a * (a - other.y) * (a - other.x) * (a - other.z)); cout << s << endl; return snum+s; } private: double x; double y; double z; }; int main() { Triangle tri1(3, 4, 5), tri2(6, 8, 10); double area1 = tri1 + tri2; cout << area1 << endl; Triangle tri3(9, 12, 15); double area2 = tri1 + tri2 + tri3; cout << area2 << endl; return 0; }
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!