1.设有下面关于点Point类的定义,请在此基础上派生出一个矩形Rectangle类,用以描述矩形的左上角的顶点和右下角的顶点,并能够计算矩形的面积,并给出测试程序。
class Point
{public:
Point(int a,int b){ x=a; y=b;}
Point(){x=0;y=0;}
int GetY(){return y;}
int GetX(){return x;}
void SetY(int b){ y=b;}
void SetX(int a){ x=a;}
private:
int x;
int y;
};
class Point
{
public:
Point(int a, int b) { x = a; y = b; }
Point() { x = 0; y = 0; }
int GetY() { return y; }
int GetX() { return x; }
void SetY(int b) { y = b; }
void SetX(int a) { x = a; }
private:
int x;
int y;
};
class Rectangle :public Point
{
public:
Rectangle(int x1, int y1, int x2, int y2) :p1(x1, y1), p2(x2, y2)
{
s = fabs((x1 - x2) * (y1 - y2));
}
void display()
{
cout << "左上角顶点为(" << p1.GetX() << "," << p1.GetY() << ")" << endl;
cout << "右下角顶点为(" << p2.GetX() << "," << p2.GetY() << ")" << endl;
cout << "面积是: " << s;
}
private:
Point p1, p2;
int s;
};
int main()
{
Rectangle rectangle(1, 1, 5, 5);
rectangle.display();
return 0;
}
2.设计一个圆类Circle和一个桌子类Table,另设计一个圆桌类Roundtable,它是前面两个类的派生类,要求编写测试程序给出输出一个圆桌的高度、面积和颜色等数据。
#include<iostream>
#include<cmath>
#define _CRT_SECURE_NO_WARNINGS //这个宏定义最好要放到.c文件的第一行
#pragma warning(disable:4996)
#include <string.h>
#define PI 3.14
class Circle
{
private:
double radius;
public:
Circle(double r = 0)
{
radius=r;
}
double Area()
{
return PI * radius * radius;
}
};
class Table
{
public:
Table(double h)
{
height = h;
}
double GetH()
{
return height;
}
private:
double height;
};
class Roundtable :public Circle, public Table
{
public:
Roundtable(double radius, double h,const char* c)
:Circle(radius), Table(h)
{
color = c;
}
string GetC()
{
return color;
}
private:
string color;
};
int main()
{
Roundtable A(5.6, 0.9, "blue");
cout << "the Basic information of the roundtable : " << endl;
cout << "面积 : " << A.Area() << endl;
cout << "高度 : " << A.GetH() << endl;
cout << "颜色 : " << A.GetC() << endl;
}
3.下面的程序可以输出ASCII字符与所对应的数字的对照表。修改下列程序,使其可以输出字母a 到z与所对应的数字的对照表。
#include<iostream>
#include<cstring>
#include <iomanip>
using namespace std;
class Table
{
public:
Table(int p)
{
i=p;
}
void ascii( );
protected :
int i;
};
void Table::ascii( )
{
int k=1;
for (; i<127; i++)
{
cout<<setw(4)<<i<<" "<<(char)i;
if ((k)%12==0)
cout<<"\n";
k++;
}
cout<<"\n";
}
class Der_table:public Table
{
public:
Der_table(int p,char *m):Table(p)
{
c = new char[strlen(m) + 1];
strcpy(c, m);
}
void print( );
protected:
char *c;
};
void Der_table::print( )
{
cout<<c<<"\n";
Table::ascii();
}
int main()
{
Der_table ob1(32,"ASCII value---char");
ob1.print();
return 0;
}
提示:修改后的main测试函数为:
int main()
{ Der_table ob('a','z',"ASCII value---char");
ob.print();
return 0;
}
#include<iostream>
#include<cmath>
#define _CRT_SECURE_NO_WARNINGS //这个宏定义最好要放到.c文件的第一行
#pragma warning(disable:4996)
#include<iostream>
#include<cstring>
#include<string.h>
#include <iomanip>
using namespace std;
class Table
{
public:
Table(char p,char q)
{
i = (int)p;
j = (int)q;
}
void ascii();
protected:
int i;
int j;
};
void Table::ascii()
{
int k = 1;
for (; i <=j; i++)
{
cout << setw(4) << i << " " << (char)i;
if ((k) % 12 == 0)
cout << "\n";
k++;
}
cout << "\n";
}
class Der_table :public Table
{
public:
Der_table(char p,char q,const char* m) :Table(p,q)
{
c = new char[strlen(m) + 1];
strcpy(c, m);
}
void print();
protected:
char* c;
};
void Der_table::print()
{
cout << c << "\n";
Table::ascii();
}
//int main()
//{
// Der_table ob1(32, "ASCII value---char");
// ob1.print();
// return 0;
//}
//提示:修改后的main测试函数为:
int main()
{
Der_table ob('a', 'z', "ASCII value---char");
ob.print();
return 0;
}
4.编写一个求出租车收费的程序,输入起始站、终止站和路程。
计费标准为:3 公里以内10元,3 公里以后每1公里加 2 元,超过 15 公里,每公里加 3元。
要求:设计一个站类Station(用于设置起始站、终止站)和路程类Mile(用于设置路程),由这两个类派生出收费类Price(用于计费)。
已知Station类和Mile类以及测试main函数如下,请编写Price类。
(可以使用string类型代替给出代码中的字符数组。)
#define Max 20
class Station
{public:
Station()
{ strcpy(from," "); strcpy(to," "); }
Station(char f[],char t[])
{ strcpy(from,f); strcpy(to,t); }
void getdata()
{
cout<<"输入起始站终止站:";
cin>>from>>to;
}
void disp()
{
cout<<"从"<<from<<"站到"<<to<<"站";
}
protected:
char from[Max];
char to[Max];
};
class Mile
{
public:
Mile(){mile=0;}
Mile(double m){mile=m;}
void getdata()
{
cout<<"输入里程:";
cin>>mile;
}
void disp()
{
cout<<"是"<<mile<<"公里";
}
protected:
double mile;
};
int main()
{
Price A; //乘车,
A.getdata(); //输入起始站和终点站
Price B("理工大学","中山公园",20); //乘车,有起始站和终点站
//A和B是Price的两种初始化方式。20是路程数。
cout<<"输出结果:"<<endl;
A.disp(); //输出此次乘车价格
B.disp(); //输出此次乘车价格
return 0;
}
#include<iostream>
#include<cmath>
#define _CRT_SECURE_NO_WARNINGS //这个宏定义最好要放到.c文件的第一行
#pragma warning(disable:4996)
// 二选一
using namespace std;
#define Max 20
class Station
{
public:
Station()
{
strcpy(from, " "); strcpy(to, " ");
}
Station(const char f[],const char t[])
{
strcpy(from, f); strcpy(to, t);
}
void getdata()
{
cout << "输入起始站终止站:";
cin >> from >> to;
}
void disp()
{
cout << "从" << from << "站到" << to << "站";
}
protected:
char from[Max];
char to[Max];
};
class Mile
{
public:
Mile() { mile = 0; }
Mile(double m) { mile = m; }
void getdata()
{
cout << "输入里程:";
cin >> mile;
}
void disp()
{
cout << "是" << mile << "公里";
}
protected:
double mile;
};
class Price :public Mile, public Station
{
public:
Price() :Station(), Mile()
{
price = 0;
}
Price(const char* f, const char* t, int m) :Station(f, t), Mile(m)
{
}
void getdata()
{
Station::getdata();
Mile::getdata();
}
void disp()
{
int m = mile;
if(m<3) price=10;
else if (m >= 3 && m <= 15)
{
price = 10 + 2 * (m - 3);
}
else
{
price = 34 + 3 * (m - 15);
}
Station::disp();
cout << "是" << price << "元";
}
private:
double price;
};
int main()
{
Price A; //乘车,
A.getdata(); //输入起始站和终点站
Price B("理工大学", "中山公园", 20); //乘车,有起始站和终点站
//A和B是Price的两种初始化方式。20是路程数。
cout << "输出结果:" << endl;
A.disp(); //输出此次乘车价格
B.disp(); //输出此次乘车价格
return 0;
}
5.设计一个小猫钓鱼的游戏程序。基本需求如下:
(1)每个小猫有自己的等级(level)和经验分(exp),每累计获得500经验分,就升一级,同时经验分清0;
(2)小猫每次只能钓一条鱼,如果钓上一条章鱼(Octopus),经验分的增加值为 2*章鱼的重量;如果钓上一条鲸鱼(Whale),经验分增加200;如果钓上一个金龟(Turtle),则等级直接升一级;如果钓上来一条鲨鱼(Shark),则在等级不变的前提下减少经验分(至多减至0),减少值为5*鲨鱼的重量。考虑到游戏的趣味性,将来可能还要增加其他类型的鱼以及相应的奖励或惩罚方法。
请根据上面的模型描述,制定合理的设计方案,请完整定义并实现小猫类,其中类的成员至少要有一个成员函数CatchFish,用来体现小猫钓鱼的行为过程。同时设计Fish类,使用继承机制从其派生出具体的鱼类。
#include<iostream>
#include<cmath>
#define _CRT_SECURE_NO_WARNINGS //这个宏定义最好要放到.c文件的第一行
#pragma warning(disable:4996)
// 二选一
using namespace std;
class Fish
{
public:
Fish(int w)
{
weight = w;
point = 0;
}
virtual int Point () const
{
return 0;
}
protected:
int weight;
int point;
};
class Octopus:public Fish
{
public:
Octopus(int w):Fish(w)
{
}
virtual int Point() const
{
cout << "cat fish Octopus" << endl;
return weight*2;
}
private:
};
class Turtle :public Fish
{
public:
Turtle(int w) : Fish(w)
{
}
virtual int Point() const
{
cout << "cat fish Turtle" << endl;
return 500;
}
private:
private:
};
class Whale:public Fish
{
public:
Whale(int w) : Fish(w)
{
}
virtual int Point() const
{
cout << "cat fish Whale" << endl;
return 200;
}
private:
};
class Shark :public Fish
{
public:
Shark(int w) : Fish(w)
{
}
virtual int Point() const
{
cout << "cat fish Shark" << endl;
return -(weight*5);
}
};
class Cat
{
public:
Cat(int le = 0, int e = 0) {
level = le;
exp = e;
}
void CatchFish(Fish& f)
{
int t= f.Point();
if (t > 0) {
level += t / 500;
exp = t % 500;
}else
{
exp += t;
if (exp < 0)exp = 0;
}
cout << "level:" << level << ' ' << "exp" << exp << endl;
}
private:
int level;
int exp;
};
int main()
{
Cat c;
Turtle t(20);
Octopus o(60);
Shark s(20);
c.CatchFish(t);
c.CatchFish(o);
c.CatchFish(s);
}