C++实验05_ 函数重载_ 虚基类
实验05(01)Point类运算符重载
题目描述
声明Point类,有坐标x,y,为整型的私有数据成员。
声明成员函数:
Point& operator++();
Point& operator+(Point);
Point(int x = 0,int y =0);
声明友元函数:
friend Point operator--(Point&,int);
friend Point operator-(Point,Point);
friend ostream& operator<<(ostream &,Point p);
注意:若一个函数返回的是对象引用,则不返回auto型对象,需要静态存储的对象, 如static类型的对象
主函数中定义Point类的三个对象:p1、p2、p,p1、p2的初始值由键盘输入,p使用默认值。依次实现运算:p=++p1; p=p2--; p=p1+p2; p=p1-p2;后将结果输出。注意结果输出要求使用类似于“cout<<p1;”的形式。
输入描述
两个点的坐标
输出描述
进行++、--、+、-运算后各点的坐标
输入样例
10 9
4 5
输出样例
p=++p1后,p的坐标为:(11,10),p1坐标为:(11,10)
p=p2--后,p的坐标为:(4,5),p2坐标为:(3,4)
p=p1+p2后,p的坐标为:(14,14)
p=p1-p2后,p的坐标为:(8,6)
#include <iostream>
using namespace std;
class Point
{
private:
int real, imag;
public:
Point(int r = 0, int i = 0)
{
real = r;
imag = i;
}
friend ostream &operator<<(ostream &, Point &);
friend istream &operator>>(istream &, Point &);
friend Point operator--(Point &, int);
friend Point operator-(Point, Point);
Point &operator++();
Point &operator+(Point);
};
ostream &operator<<(ostream &output, Point &obj)
{
output << "(" << obj.real << "," << obj.imag << ")";
return output;
}
istream &operator>>(istream &input, Point &obj)
{
input >> obj.real;
input >> obj.imag;
return input;
}
Point operator-(Point a, Point b)
{
Point temp(0, 0);
temp.real = a.real - b.real;
temp.imag = a.imag - b.imag;
return temp;
}
Point &Point ::operator++() //单目运算符成员函数的返回值是this指针比如是return (*this)
{
real++;
imag++;
return (*this);
}
Point &Point ::operator+(Point a)
{
static Point temp(0, 0);
temp.real = real + a.real;
temp.imag = imag + a.imag;
return temp;
}
Point operator--(Point &op, int) //后置
{
Point temp;
temp = op;
op.imag--;
op.real--;
return temp;
}
int main()
{
Point p1, p2, p;
cin >> p1 >> p2;
p = ++p1;
cout << "p=++p1后,p的坐标为:" << p << ",p1坐标为:" << p1 << endl;
p = p2--;
cout << "p=p2--后,p的坐标为:" << p << ",p2坐标为:" << p2 << endl;
p = p1 + p2;
cout << "p=p1+p2后,p的坐标为:" << p << endl;
p = p1 - p2;
cout << "p=p1-p2后,p的坐标为:" << p << endl;
return 0;
}
实验05(02)抽象类与纯虚函数
题目描述
定义一个抽象类Shape,有一个纯虚函数calArea(),返回值为double型。由Shape类派生出4种几何图形:Triangle、Rectangle、Square、Circle,各自新增的数据成员均为double型。
定义两个普通的重载函数fun():返回值均为void,形参分别是抽象类的对象指针、抽象类的对象引用,在函数中通过指针或引用调用虚函数calArea(),输出结果。
主函数中,分别定义Triangle、Rectangle、Square、Circle类的对象,各初始值由键盘输入,分别调用fun(),输出各种图形的面积。
PI:3.1415926
输入描述
三角形的底和高
长方形的长和宽
正方形的连长
圆的半径
输出描述
分别调用fun()之后的结果
输入样例
5 7
4 5
6.7
3.4
输出样例
计算三角形面积:
通过基类指针调用,面积为:17.5
通过基类对象引用调用,面积为:17.5
计算长方形面积:
通过基类指针调用,面积为:20
通过基类对象引用调用,面积为:20
计算正方形面积:
通过基类指针调用,面积为:44.89
通过基类对象引用调用,面积为:44.89
计算圆形面积:
通过基类指针调用,面积为:36.3168
通过基类对象引用调用,面积为:36.3168
#include <iostream>
#define PI 3.1415926
using namespace std;
class Shape
{
public:
virtual double calArea() = 0;
};
void fun(Shape *a)
{
cout << "通过基类指针调用,面积为:" << a->calArea() << endl;
}
void fun(Shape &a)
{
cout << "通过基类对象引用调用,面积为:" << a.calArea() << endl;
}
class Triangle : public Shape
{
public:
Triangle(double x, double y)
{
a = x;
b = y;
}
double calArea()
{
return 0.5 * a * b;
}
private:
double a, b;
};
class Rectangle : public Shape
{
public:
Rectangle(double x, double y)
{
a = x;
b = y;
}
double calArea()
{
return a * b;
}
private:
double a, b;
};
class Square : public Shape
{
public:
Square(double x)
{
a = x;
}
double calArea()
{
return a * a;
}
private:
double a;
};
class Circle : public Shape
{
public:
Circle(double x)
{
a = x;
}
double calArea()
{
return a * a * PI;
}
private:
double a;
};
int main()
{
Shape *ptr;
double a, b, c, d;
double x, r;
cin >> a >> b >> c >> d >> x >> r;
Triangle t(a, b);
ptr = &t;
Shape &o = t;
cout << "计算三角形面积:" << endl;
fun(ptr);
fun(o);
Rectangle re(c, d);
ptr = &re;
Shape &o1 = re;
cout << "计算长方形面积:" << endl;
fun(ptr);
fun(o1);
Square s(x);
ptr = &s;
Shape &o2 = s;
cout << "计算正方形面积:" << endl;
fun(ptr);
fun(o2);
Circle cir(r);
ptr = ○
Shape &o3 = cir;
cout << "计算圆形面积:" << endl;
fun(ptr);
fun(o3);
return 0;
}
实验05(03)虚函数与虚基类
题目描述
在习题4-3的基础上,编程实现:本题的类间关系如图5-1所示(学习通中的实验5文档中)。
定义一个人员类Person,数据成员包括编号、姓名、性别、家庭住址、联系电话。性别用char型实现,’f’表示女性,’m’表示男性,其它用string类型。成员函数包括:(1)构造函数;(2)输出函数print(),输出一个人员的全部描述信息,并定义为虚函数。
定义一个学生类Student,为Person类的公有派生类。新增的数据成员包括数学、物理、英语、程序设计四门课成绩,各门课成绩为整型。新增的成员函数包括:(1)设置数学、物理、外语、程序设计四门课成绩,函数原型为void setScore(char tag, int score);当tag的值分别为’m’、’p’、’e’和’c’时,分别设置数学、物理、英语、程序设计四门课成绩。1)构造函数;(2) 输出函数print(),输出一个学生的全部信息,注意在此函数中调用基类中输出人员的全部描述信息的函数。
定义一个教师类Teacher,为Person类的公有派生类。新增的数据成员包括:职称、工资(职称为string类型,工资为double型)。新增的成员函数包括:(1) 构造函数;(2) 输出函数print(),输出一个教师的全部描述信息,注意在此函数中调用基类中输出人员的全部描述信息的函数。
由Student类和Teacher类公有派生子类TA(助教博士生)类,添加数据成员:专业(string类型),添加构造函数、输出函数(输出一个TA对象的全部信息)。
定义一个普通函数fun(),形参为Person类的指针,利用C++的多态性,调用虚函数实现输出各对象的信息。
主函数中定义分别Student、Teacher、TA类的对象各一个,其各数据成员的初始值由键盘输入,调用fun()输出Student对象的全部信息;修改Student对象的成绩,数据由键盘输入,再调用fun()输出Student对象的全部信息。调用fun()输出Teacher、TA类的对象的全部信息。
注意虚基类的使用。
输入描述
四行
学生类对象的全部初值
成绩修改的代号和成绩
教师类对象的全部初值
TA类对象的全部初值
输出描述
学生对象的全部信息
学生对象修改成绩后的全部信息
教师对象的全部信息
TA对象的全部信息
输入样例
1001 刘玄德 m 河北涿州 12345678999 87 90 85 88
m 92
2031001 诸葛亮 m 徐州琅琊 13344556677 副教授 5500
3045866 赵子龙 m 常山真定 12345678988 87 90 85 88 讲师 3500 计算机科技与技术
输出样例
学生信息:
编 号:1001
姓 名:刘玄德
性 别:m
家庭住址:河北涿州
联系电话:12345678999
数 学:87
物 理:90
英 语:85
程序设计:88
编 号:1001
姓 名:刘玄德
性 别:m
家庭住址:河北涿州
联系电话:12345678999
数 学:92
物 理:90
英 语:85
程序设计:88
教师信息:
编 号:2031001
姓 名:诸葛亮
性 别:m
家庭住址:徐州琅琊
联系电话:13344556677
职 称:副教授
工 资:5500
TA信息:
编 号:3045866
姓 名:赵子龙
性 别:m
家庭住址:常山真定
联系电话:12345678988
数 学:87
物 理:90
英 语:85
程序设计:88
职 称:讲师
工 资:3500
专 业:计算机科技与技术(中文冒号)
#include <iostream>
#include <string>
using namespace std;
class Person
{
public:
Person(string a, string b, char c, string d, string e);
void set_number(string a);
void set_name(string b);
void set_sex(char c);
void set_address(string d);
void set_phone_number(string e);
virtual void print()
{
cout << "编 号:" << number << endl;
cout << "姓 名:" << name << endl;
cout << "性 别:" << sex << endl;
cout << "家庭住址:" << address << endl;
cout << "联系电话:" << phone_number << endl;
}
private:
string number;
string name;
char sex;
string address;
string phone_number;
};
Person::Person(string a, string b, char c, string d, string e)
{
number = a;
name = b;
sex = c;
address = d;
phone_number = e;
}
void Person::set_number(string a)
{
number = a;
}
void Person::set_name(string b)
{
name = b;
}
void Person::set_sex(char c)
{
sex = c;
}
void Person::set_address(string d)
{
address = d;
}
void Person::set_phone_number(string e)
{
phone_number = e;
}
class Student : virtual public Person
{
public:
Student(string a, string b, char c, string d, string e, int f, int g, int h, int i);
void setScore(char tag, int score);
virtual void print()
{
Person::print();
cout << "数 学:" << math << endl;
cout << "物 理:" << physical << endl;
cout << "英 语:" << english << endl;
cout << "程序设计:" << c_lan << endl;
}
void cj()
{
cout << "数 学:" << math << endl;
cout << "物 理:" << physical << endl;
cout << "英 语:" << english << endl;
cout << "程序设计:" << c_lan << endl;
}
private:
int math, physical, english, c_lan;
};
Student::Student(string a, string b, char c, string d, string e, int f, int g, int h, int i) : Person(a, b, c, d, e)
{
math = f;
physical = g;
english = h;
c_lan = i;
}
void Student::setScore(char tag, int score)
{
switch (tag)
{
case 'm':
math = score;
break;
case 'p':
physical = score;
break;
case 'e':
english = score;
break;
case 'c':
c_lan = score;
break;
}
}
class Teacher : virtual public Person
{
public:
Teacher(string a, string b, char c, string d, string e, string f, double g);
void set_title(string f);
void set_wages(double g);
virtual void print();
void cz()
{
cout << "职 称:" << title << endl;
cout << "工 资:" << wages << endl;
}
private:
string title;
double wages;
};
Teacher::Teacher(string a, string b, char c, string d, string e, string f, double g) : Person(a, b, c, d, e)
{
title = f;
wages = g;
}
void Teacher::set_title(string f)
{
title = f;
}
void Teacher::set_wages(double g)
{
wages = g;
}
void Teacher::print()
{
Person::print();
cout << "职 称:" << title << endl;
cout << "工 资:" << wages << endl;
}
class Ta : virtual public Student, Teacher
{
public:
Ta(string a, string b, char c, string d, string e, int f, int g, int h, int i, string ch, double gz, string zh) : Student(a, b, c, d, e, f, g, h, i), Teacher(a, b, c, d, e, ch, gz), Person(a, b, c, d, e)
{
zhuan = zh;
}
virtual void print()
{
Person::print();
Student::cj();
Teacher::cz();
cout << "专 业:" << zhuan << endl;
}
private:
string zhuan;
};
void fun(Person *a)
{
a->print();
}
int main()
{
string number_1;
string name_1;
char sex_1;
string address_1;
string phone_number_1;
string number_2;
string name_2;
char sex_2;
string address_2;
string phone_number_2;
int math, physical, english, c_lan;
string number_3;
string name_3;
char sex_3;
string address_3;
string phone_number_3;
int math_2, physical_2, english_2, c_lan_2;
string title, title1, zhuan;
double wages, wages1;
char tag;
int sc;
string phone_number;
cin >> number_1 >> name_1 >> sex_1 >> address_1 >> phone_number_1;
cin >> math >> physical >> english >> c_lan;
Student zp(number_1, name_1, sex_1, address_1, phone_number_1,
math, physical, english, c_lan);
cout << "学生信息:" << endl;
zp.print();
cin >> tag >> sc;
zp.setScore(tag, sc);
zp.print();
cin >> number_2 >> name_2 >> sex_2 >> address_2 >> phone_number_2;
cin >> title >> wages;
Teacher ls(number_2, name_2, sex_2, address_2, phone_number_2, title, wages);
cout << "教师信息:" << endl;
ls.print();
cin >> number_3 >> name_3 >> sex_3 >> address_3 >> phone_number_3;
cin >> math_2 >> physical_2 >> english_2 >> c_lan_2;
cin >> title1 >> wages1;
cin >> zhuan;
Ta zhujiao(number_3, name_3, sex_3, address_3, phone_number_3,
math_2, physical_2, english_2, c_lan_2, title1, wages1, zhuan);
cout << "TA信息:" << endl;
zhujiao.print();
}
版权声明:本文为weixin_44179485原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。