本节实验课为第六章多态性与虚函数的内容。
- 定义point类,有坐标x、y两个成员变量;对point类重载“++”、“--”运算符,实现对坐标值的改变。
#include <iostream>
using namespace std;
class point
{
private:
int x,y;
public:
point(int a, int b)
{
x = a;
y = b;
}
void show()
{
cout<<"("<<x<<","<<y<<")"<<endl;
}
void operator ++()
{
x++;
cout<<"("<<x<<","<<y<<")"<<endl;
}
void operator ++(int)
{
y++;
cout<<"("<<x<<","<<y<<")"<<endl;
}
void operator --()
{
x--;
cout<<"("<<x<<","<<y<<")"<<endl;
}
void operator --(int)
{
y--;
cout<<"("<<x<<","<<y<<")"<<endl;
}
};
int main()
{
point t1(1,2);
cout<<"当前位置:"<<endl;
t1.show();
cout<<"横着前进一步:"<<endl;
++t1;
cout<<"竖着前进一步:"<<endl;
t1++;
cout<<"横着后退一步:"<<endl;
--t1;
cout<<"竖着后退一步:"<<endl;
t1--;
return 0;
}
2.定义一个车(vehicle)基类,有Run、stop等成员函数,由此派生出自行车(bicycle)类、汽车类(motorcar),从bicycle和motorcar派生出摩托车(motorcycle)类,他们都有run、stop等成员函数,观察虚函数的作用。
#include <iostream>
using namespace std;
class vehicle
{
protected:
int x,num;
public:
vehicle(int a, int b)
{
x = a;
num = b;
}
virtual void run()
{
x++;
}
virtual void stop()
{
cout<<"当前位置:"<<x<<", 编号:"<<num<<endl;
}
};
class bicycle :public vehicle
{
protected:
int money;
public:
bicycle(int a, int b,int c):vehicle(a,b)
{
money = c;
}
void run()
{
vehicle::run();
}
void stop()
{
cout<<"当前位置:"<<x<<",编号:"<<num<<"\n价格:"<<money<<endl;
}
};
class motorcar :public vehicle
{
protected:
int age;
public:
motorcar(int a, int b, int c):vehicle(a,b)
{
age = c;
}
void run()
{
vehicle::run();
}
void stop1()
{
cout<<"车龄:"<<age<<endl;
}
void stop()
{
cout<<"当前位置:"<<x<<",编号:"<<num<<"\n车龄:"<<age<<endl;
}
};
class motorcycle : public bicycle ,public motorcar
{
private:
string name;
public:
motorcycle(int a, int b, int c, int d, string e) :bicycle(a, b, c),motorcar(a, b, d)
{
name = e;
}
void run()
{
bicycle::run();
}
void stop()
{
bicycle::stop();
motorcar::stop1();
cout<<"名字:"<<name<<endl;
}
};
int main()
{
motorcycle t1(1,1,4,1000,"lisa");
bicycle b1(2, 2,200);
motorcar m1(3, 3, 5);
t1.run();
t1.stop();
m1.run();
m1.stop();
b1.run();
b1.stop();
return 0;
}
3.编写程序,定义抽象基类container,由此派生出两个派生类球体类sphere、圆柱体类cylinder,分别用虚函数计算表面积和体积。
(1)球体的表面积为:4,球体体积为:
,圆柱表面积为:2
r(h+r),圆柱体体积为:
(2)定义相应的对象,编写主函数测试。
#include <iostream>
using namespace std;
#define pi 3.14
class container
{
public:
virtual double squal()= 0;
virtual double area()= 0;
};
class sphere :public container
{
private:
double r;
public:
sphere(double a)
{
r = a;
}
double squal()
{
double c;
c = 4*pi*r*r;
return c;
}
double area()
{
double c;
c = 4*pi*r*r*r/3.0;
return c;
}
};
class cylinder :public container
{
protected:
double h,r;
public:
cylinder(double a, double b)
{
r = a;
h = b;
}
double squal()
{
double c;
c = 2*pi*r*(h+r);
return c;
}
double area()
{
double c;
c = pi*r*r*h;
return c;
}
};
int main()
{
container *s1 = new sphere(3);
container *c1 = new cylinder(6,3);
cout<<"球体的表面积体积分别为:"<<endl;
cout<<s1->squal()<<" "<<s1->area()<<endl;
cout<<"圆柱体的表面积体积分别为:"<<endl;
cout<<c1->squal()<<" "<<c1->area()<<endl;
return 0;
}
4.定义一个Animal,该类中可以存放动物的名字,并有一个Identity函数用于显示动物的种类。定义两个类Cat和Dog,都继承自Animal,并重载Identity函数,不但要显示动物的种类,还要显示动物的名字。
#include <iostream>
using namespace std;
class Animal
{
protected:
string name;
public:
Animal(string n)
{
name = n;
}
void Identity() {}
};
class Cat :public Animal
{
public:
Cat(string n):Animal(n)
{}
void display()
{
cout<<"名字:"<<name<<endl;
}
void Identity()
{
cout<<"种类:cat"<<endl;
}
};
class Dog :public Animal
{
public:
Dog(string n):Animal(n)
{}
void display()
{
cout<<"名字:"<<name<<endl;
}
void Identity()
{
cout<<"种类:dog"<<endl;
}
};
int main()
{
Cat c("tom");
Dog d("john");
c.display();
c.Identity();
d.display();
d.Identity();
return 0;
}
5.请编写一个抽象类Shape,在此基础上派生出类Rectangle和Circle,二者都有计算对象面积的函数GetArea()和计算周长函数GetPerim()。
#include <iostream>
using namespace std;
#define pi 3.14
class Shape
{
public:
virtual void GetArea() = 0;
virtual void GetPerim() = 0;
};
class Rectangle:public Shape
{
private:
int length,width;
public:
Rectangle(int a, int b)
{
length = a;
width = b;
}
void GetArea()
{
cout<<"长方形面积为:"<<width*length<<endl;
}
void GetPerim()
{
int c;
c = 2*(width + length);
cout<<"长方形周长为:"<<c<<endl;
}
};
class Circle:public Shape
{
private:
double r;
public:
Circle(double a)
{
r = a;
}
void GetArea()
{
cout<<"圆的面积为:"<<pi*r*r<<endl;
}
void GetPerim()
{
double c;
c = 2*pi*r;
cout<<"圆的周长为:"<<c<<endl;
}
};
int main()
{
Rectangle s(2,3);
Circle c(3);
s.GetArea();
s.GetPerim();
c.GetArea();
c.GetPerim();
return 0;
}
3176

被折叠的 条评论
为什么被折叠?



