西电C++上机实验三

C++上机实验三

本人就一枚大三计科小辣鸡,刚好用csdn记录自己的学习记录,如果有什么错误,希望可以指出,谢谢啦!
实验题目如下:

  1. 定义一个复数类Complex,重载运算符“+”,“-”,“*”,“/”,使之能用于复数的加、减、乘、除。运算符重载函数作为Complex类的成员函数。编程序,分别求两个复数之和、差、积和商。
  2. 对于2行3列矩阵,重载流插入运算符“<<”和流提取运算符“>>”,使之能用于矩阵的输入和输出。
  3. 定义Time类和Date类,Time类为Date类的友元类,通过Time类中的display函数引用Date类对象的私有数据,输出年、月、日和时、分、秒。
    4.分别定义Teacher(教师)类和Cadre(干部)类,采用多继承方式由这两个类派生出新类Teacher_Cadre(教师兼干部)。要求:
    (1)在两个基类中都包含姓名、年龄、性别、地址、电话等数据成员。
    (2)在Teacher类中还包含数据成员titile(职称),在Cadre类中还包含数据成员post(职务),在Teacher_Cadre类中还包含数据成员wages(工资)。
    (3)对两个基类中的姓名、年龄、性别、地址、电话等数据成员用相同的名字,在引用这些数据成员时,指定作用域。
    (4)在类体中声明成员函数,在类外定义成员函数。
    (5)在派生类Teacher_Cadre的成员函数show中调用Teacher类中的display函数,输出姓名、年龄、性别、职称、地址、电话,然后再用cout语句输出职务与工资。
    5.写一个程序,定义抽象基类Shape,由它派生出5个派生类:Circle(圆形)、Square(正方形)、Rectangle(矩形)、Trapezoid(梯形)、Triangle(三角形)。用虚函数分别计算几种图形面积,并求它们的和。要求用基类指针数组,使它的每一个元素指向一个派生类对象。

第一题

本题考查运算符的重载,类内的重载形式为:
<数据类型> operator <运算符>(){
}
举例:

Complex operator +(Complex &a){
		Complex t(0,0);
		t.shi = a.shi +shi;
		t.xu = a.xu + xu;
		return t;
	}

做 Complex t = a+b运算时,相当于t = a.operator +(b);
特别指出,此种实现与重载为类的友元函数的表达上有一定的区别。
得知以后,此题就不难实现

#include<iostream>
using namespace std;
 
class Complex{
public:
	Complex(int x,int y){
	shi = x;
	xu = y;
	}
	Complex operator +(Complex &a){
		Complex t(0,0);
		t.shi = a.shi +shi;
		t.xu = a.xu + xu;
		return t;
	}
	Complex operator -(Complex &a){
		Complex t(0,0);
		t.shi = shi - a.shi;
		t.xu = xu - a.xu;
		return t;
	}
	Complex operator *(Complex &a){
		Complex t(0,0);
		t.shi = shi * a.shi -xu*a.xu;
		t.xu = shi*a.xu + xu*a.shi;
		return t;
	}
	Complex operator /(Complex &a){
		Complex t(0,0);
		t.shi = (shi*a.shi+xu*a.xu)/(a.shi*a.shi + a.xu*a.xu);
		t.xu = (xu*a.shi - shi*a.xu)/(a.shi*a.shi + a.xu*a.xu);
		return t;
	}
	void show(){
		if(xu > 0)
		cout<<shi<<"+"<<xu<<"i"<<endl;
		else if(xu <0)
		cout<<shi<<xu<<"i"<<endl;
		else
		cout<<shi<<endl;
	}
private:
	float shi,xu;
};

int main(){
	Complex a(5,3);
	Complex b(2,1);
	Complex t = a/b;
	t.show();
	return 0;
}

第二题

重载输入输出运算符
格式:

//输入流
friend istream &operator >>(istream&a ,className &b){}
//输出流
friend ostream &operator <<(ostream&a ,className &b){}

因此,实现为:

#include<iostream>
using namespace std;
class matrix{
public:
	matrix(){ //构造全零的矩阵 
		for(int i = 0;i < 3;i++){
			li1[i] = 0;
			li2[i] = 0;
		}
	}
	friend istream & operator >>(istream &is,matrix &cc){
		for(int i = 0;i < 3;i++){
			is >> cc.li1[i] ;
		}
		for(int i = 0;i < 3;i++){
			is >>cc.li2[i];
		}
		return is;
	}
	friend ostream & operator <<(ostream &is,matrix &cc){
		for(int i = 0;i < 3;i++){
			is << cc.li1[i] <<" ";
		}
		cout<<endl;
		for(int i = 0;i < 3;i++){
			is<<cc.li2[i] <<" ";
		}
		return is;
	}
private:
	int li1[3];
	int li2[3];
}; 

int main(){
	matrix a,b;
	cin>>a;
	cout<<a;
	return 0;
}

第三题

友元类的考察,就是私有数据也可以用

#include<iostream>
using namespace std;

class Date{
public:
	Date(int year,int month,int day,int hour,int min,int second){
		this->year = year;
		this->month = month;
		this->day = day;
		this->hour = hour;
		this->min = min;
		this->second = second;
	}
	friend class Time;
private:
	int year,month,day,hour,min,second;
};

class Time{
public:
	void show(Date &a){
		cout<<"现在时间为"<<a.year<<"年"<<a.month<<"月"<<a.day<<"日"<<a.hour<<"点"<<a.min<<"分"<<a.second<<"秒"<<endl;
	}
};

int main(){
	Date a(1999,11,2,23,11,56);
	Time t;
	t.show(a);
	return 0;
}

第四题

类的继承
格式:

class A:public B{};

这个图比较重要
在这里插入图片描述
除此之外,继承后构造方法也是一个难点
格式如下:
在这里插入图片描述
举例:

Rectangle(float x,float y,float w,float h):Point(x,y){W=w;H=h;}
//或
Rectangle(float x,float y,float w,float h):Point(x,y),W(w),H(h){}

解题如下:

#include<iostream>
using namespace std;

class Teacher{
public:
	Teacher(char *nam,int ag,char *se,char *addre,int tel,char *titil){
		name = nam;
		age = ag;
		sex = se;
		addres = addre;
		tele = tel;
		titile = titil;
	}
	void display(){
		cout<<"姓名:"<<name<<endl<<"年龄:"<<age<<endl<<"性别:"<<sex<<endl<<"地址:"<<addres<<endl<<"电话:"<<tele<<endl<<"职称:"<<titile<<endl; 
	}
private:
	char *name;
	int age;
	char *sex;
	char *addres;
	int tele;
	char *titile;
};
class Cadre{
public:
	Cadre(char *nam,int ag,char *se,char *addre,int tel,char *pos){
		name = nam;
		age = ag;
		sex = se;
		addres = addre;
		tele = tel;
		post = pos;
	}
private:
	char *name;
	int age;
	char *sex;
	char *addres;
	int tele;
protected:
	char *post;	
};
class Teacher_Cadre:public Teacher,public Cadre{
public:
	Teacher_Cadre(char *name,int age,char *sex,char *addres,int tele,char *titile,char *post,int wage):Teacher(name,age,sex,addres,tele,titile),Cadre(name,age,sex,addres,tele, post){
		this->wage = wage;
	}
	void show(){
		Teacher::display();
		cout<<"职务:"<<Cadre::post<<endl<<"工资:"<<wage<<endl;
	}
private:
	int wage;	
};
int main(){
	Teacher_Cadre a("jasur",21,"male","hjbvuvbu",1351999,"teacher","cadre",1800);
	a.show();
	return 0;
}

第五题

第五题关于抽象基类和虚函数,知识点下一次补充
解题如下

#include<iostream>
using namespace std;

class shape{
public:
	virtual void getArea() = 0;
};
class circle:public shape{
public:
	circle(float r){
		this->r = r;
	}
	virtual void getArea(){
		cout<<"圆的面积为:"<<3.14*r*r<<endl;
	}
private:
	float r;
};
class square:public shape{
private:
	float a;
public:
	square(float a){
		this->a = a;
	}
	virtual void getArea(){
		cout<<"正方形的面积为:"<<a*a<<endl;
	}
	
};
class rectangle:public shape{
private:
	float height,weight;
public:
	rectangle(float a,float b){
		height  =a;
		weight = b;
	}
	virtual void getArea(){
		cout<<"矩形的面积为:" <<height*weight<<endl; 
	}
};
class trapezoid:public shape{
private:
	float up,down,height;  //上底,下底,高 
public:
	trapezoid(float a,float b,float c){
		up = a;
		down = b;
		height = c;
	}
	virtual void getArea(){
		cout<<"梯形的面积为:"<<(up+down)*height/2<<endl;
	}
};
class triangle:public shape{
private:
	float height,weight;
public:
	triangle(float a,float b){
	height = a;
	weight = b;
	}
	virtual void getArea(){
		cout<<"三角形的面积为:"<<height*weight/2<<endl;
	}
};
void displayArea(shape **p,int n){
	for(int i = 0;i <n;i++){
		p[i]->getArea();
	}
}
int main(){
	shape *a[5];
	circle cir(1.2);
	square squ(4);
	rectangle rect(12,4);
	trapezoid tra(1,2,15);
	triangle tri(12,12);
	a[0] = &cir;
	a[1] = &squ;
	a[2] = &rect;
	a[3] = &tra;
	a[4] = &tri;
	displayArea(&*a,5);
	return 0;
}

如果hxdm读到了这里,给我点点关注吧,我会分享一些自己的学习经历!

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 游动-白 设计师:上身试试 返回首页