实验目的:
熟悉c++的类、对象。
熟悉c++的重载、继承。
实验步骤与内容:
1、建立一个学生类,一个对象数组,内放5个学生的数据(学号、成绩),用指针指向数组首元素,输出第1,3,5学生的数据。
- 建立一个函数bestStudent,用指向对象的指针作函数参数,在函数中找出5个学生中成绩最高者,并输出其学号。
- 在主函数main()中,调用bestStudent
#include <iostream>
using namespace std;
class Student{
public:
int number;
int grade;
Student(int a,int b){
number=a;
grade=b;
}
};
void bestStudent(Student *p){
int b;
int a=p->grade;
for(int i=1;i<5;i++){
if(a<(p+i)->grade){
a=(p+i)->grade;
b=i;
}
}
cout<<"最好的成绩是:"<<a<<" 其学号是:"<<(*(p+b)).number<<endl;
}
int main()
{
Student stu[5]={
Student(1101,60),
Student(1102,80),
Student(1103,95),
Student(1104,59),
Student(1105,72)
};
//指针指向数组首元素
Student *p=&stu[0];
//输出第1,3,5学生的数据
cout<<"第1个学生 学号:"<<p->number<<" 成绩:"<< p->grade<<endl;
cout<<"第3个学生 学号:"<<(p+2)->number<<" 成绩:"<< (p+2)->grade<<endl;
cout<<"第5个学生 学号:"<<(p+4)->number<<" 成绩:"<< (p+4)->grade<<endl;
//调用bestStudent
bestStudent(p);
return 0;
}
(测试结果如下:)
2.、定义一个复数类Complex,并实现:
- 重载运算符“+”,使之能用于复数的加法运算。将运算符函数重载为非成员、非友元的普通函数。
- 重载运算符“-”,使之能用于复数的减法运算。将运算符函数重载为类的友元函数。
- 重载运算符“*”,“/”,使之能用于复数的乘、除。运算符重载函数作为类的成员函数。
- 重载运算符“+”,使之能用于复数的加法运算。参加运算的两个运算量有一个是类对象,有一个是整数,顺序任意。例如i+c1, c1+i均合法(设i为整数,c1,c2为复数)。
- 实现实部自增、自减(都可以前置、后置)的运算
- 重载流插入运算符“<<”和流提取运算符“>>”,使之能用于复数的输入和输出。
- 处理一个复数与一个double数相加的运算(与复数的实部相加),结果存放在一个double型的变量d1中,输出d1的值。在成员函数中包含重载类型转换运算符: operator double( ) { return real;}
- 在主程序中分别调用以上复数运算,并输出运算结果。在调用的地方加上注释,说明要验证什么运算。
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
class Complex{
public:
Complex(){real=0;imag=0;}//定义构造函数
Complex(double r,double i){real=r;imag=i;}//构造函数重载
double get_real(){return real;}
double get_imag(){return imag;}
//将运算符-函数重载为类的友元函数
friend Complex operator - (Complex &c1,Complex &c2);
//将运算符* /重载函数作为类的成员函数
Complex operator * (Complex &c2);
Complex operator / (Complex &c2);
//参加运算的两个运算量有一个是类对象,有一个是整数,顺序任意
Complex operator +(int &i);
friend Complex operator +(int &i,Complex &c);
//实部自增、自减(都可以前置、后置)的运算
Complex operator ++();
Complex operator ++(int);
Complex operator --();
Complex operator --(int);
//重载流插入运算符“<<”和流提取运算符“>>”用于复数的输入和输出
friend istream& operator >> (istream&,Complex&);
friend ostream& operator << (ostream&,Complex&);
//处理一个复数与一个double数相加的运算
Complex(double r){real=r;imag=0;}
operator double(){return real;}
void display(); //声明输出函数
private:
double real;//实部
double imag;//虚部
};
Complex operator + (Complex &c1,Complex &c2){//将运算符+函数重载为非成员、非友元的普通函数
Return Complex(c1.get_real()+c2.get_real(),c1.get_imag()+c2.get_imag());
}
Complex operator -(Complex &c1,Complex &c2){//定义重载运算符-的函数
return Complex(c1.real-c2.real,c1.imag-c2.imag);
}
Complex Complex::operator *(Complex &c2){//定义重载运算符*的函数
double a=real,b=imag,c=c2.real,d=c2.imag;
return Complex((a*c-b*d),(a*d+b*c));
}
Complex Complex::operator /(Complex &c2){//定义重载运算符/的函数
double a=real,b=imag,c=c2.real,d=c2.imag;
return Complex((a*c+b*d)/(c*c+d*d),(b*c-a*d)/(c*c+d*d));
}
//定义含整数的重载运算符+的函数
Complex Complex::operator +(int &i){
return Complex(real+i,imag);
}
Complex operator+(int &i,Complex &c){
return Complex(i+c.real,c.imag);
}
//定义实部自增、自减的函数
Complex Complex::operator ++(){
return Complex(++real,imag);
}
Complex Complex::operator ++(int){
return Complex(real++,imag);
}
Complex Complex::operator --(){
return Complex(--real,imag);
}
Complex Complex::operator --(int){
return Complex(real--,imag);
}
void Complex::display(){//定义输出函数
cout<<"("<<real<<","<<imag<<"i)"<<endl;
}
//定义重载流插入运算符和流提取运算符的函数
ostream& operator << (ostream& output, Complex& c){
output<<"("<<c.real;
if(c.imag>=0) output<<"+";
output<<c.imag<<"i)"<<endl;
return output;
}
istream & operator>>( istream & input,Complex & c){
string s;
input >> s; //将"a+bi"作为字符串读入
int pos = s.find("+",0);
string sTmp = s.substr(0,pos); //分离出代表实部的字符串
c.real = atof(sTmp.c_str());//将const char*指针指向的内容转换成float
sTmp = s.substr(pos+1, s.length()-pos-2); //分离出代表虚部的字符串
c.imag = atof(sTmp.c_str());
return input;
}
int main(){
Complex c1(3,4),c2(5,-2),c3;
cout<<"c1 = ";c1.display();
cout<<"c2 = ";c2.display();
//(1)
cout<<"验证复数的加法运算:"<<endl;
c3=c1+c2;
cout<<"c1 + c2 = ";c3.display();
//(2)
cout<<"验证复数的减法运算:"<<endl;
c3=c1-c2;
cout<<"c1 - c2 = ";c3.display();
//(3)
cout<<"验证复数的乘法运算:"<<endl;
c3=c1*c2;
cout<<"c1 * c2 = ";c3.display();
cout<<"验证复数的除法运算:"<<endl;
c3=c1/c2;
cout<<"c1 / c2 = ";c3.display();
//(4)
cout<<"验证复数的加法(含整数i的)运算:"<<endl;
int i;
cout<< "请输入整数i的值:";
cin>>i;
cout<<"c1 + i = ";(c1+i).display();
cout<<"i + c1 = ";(i+c1).display();
//(5)
cout<<"验证复数的自增(前置)运算:"<<endl;
cout<<"++c1 = ";(++c1).display();
cout<<"验证复数的自增(后置)运算:"<<endl;
cout<<"c1++ = ";(c1++).display();
cout<<"验证复数的自减(前置)运算:"<<endl;
cout<<"--c1 = ";(--c1).display();
cout<<"验证复数的自减(后置)运算:"<<endl;
cout<<"c1-- = ";(c1--).display();
//(6)
cout<<"验证流插入运算符和流提取运算符用于复数的输入和输出:"<<endl;
cout<<"请以复数形式输入c1,c2:" <<endl;
cin>>c1>>c2;
cout<<"c1 = "<<c1;
cout<<"c2 = "<<c2;
//(7)
cout<<"验证复数与double数相加的运算:"<<endl;
cout<<"请输入double数d的值:";
double d;
cin>>d;
Complex c=Complex(d);
double d1=(double)c+c1;
cout<<"d1 = d + c1 = "<<d1;
return 0;
}
(测试结果如下:)
3、编写一个程序,声明抽象基类Shape,由它派生出3个派生类: Circle(圆形)、Rectangle(矩形)、Triangle(三角形),用一个函数printArea分别输出以上三者的面积,3个图形的数据在定义对象时给定。
#include <iostream>
#include <math.h>
using namespace std;
class Shape//声明抽象基类
{ public:
virtual void printArea() const=0;
};
class Circle:public Shape
{
private:
int radius;
public:
Circle(int radius):Shape(){
this-> radius=radius;
//printf("%d %d",r,radius);
}
virtual void printArea() const{
printf("圆形面积为%.2f\n",(double)radius*radius*M_PI);
}
};
class Rectangle:public Shape
{
private:
int length,width;
public:
Rectangle(int length,int width):Shape(){
this->length=length;
this->width=width;
}
virtual void printArea() const{
printf("矩形面积为%d\n",length*width);
}
};
class Triangle:public Shape
{
private:
int bottom,height;
public:
Triangle(int bottom,int height){
this->bottom=bottom;
this->height=height;
}
virtual void printArea()const{
printf("三角形面积为%.1f\n",(double)bottom*height/2);
}
};
int main(){
//圆形
Circle c=Circle(3);
c.printArea();
//矩形
Rectangle r=Rectangle(3,4);
r.printArea();
//三角形
Triangle t=Triangle(3,3);
t.printArea();
return 0;
}
(测试结果如下:)