一、实验目的:
1.理解运算符重载的概念和实质,掌握运算符重载函数的定义方法,掌握运算符重载为类的成员函数和友元函数的方法。
2.掌握虚函数的定义方法及其在实现多态性中的应用,理解静态连编和动态链编的区别。
二、实验内容:
1.已知基类:
class Base{
public : virtual void I_am(){cout<<”Base”<<endl;}
};
要求:
(1)从Base类中派生出两个类,分别定义I_am()函数,实现输出自己类的名字。
(2)主函数中创建者3个类的对象来调用I_am()函数,再利用Base的指针数组通过循环语句来调用这3个对象的I_am()函数。
#include< iostream>
#include< string>
using namespace std;
class base
{
public:
base(){}
virtual void i_am()
{
cout<<"I am base0"<<endl;
}
};
class base1:public base
{
public:
base1(){}
void i_am()
{
cout<<"I am base1"<<endl;
}
};
class base2:public base
{
public:
base2(){}
void i_am()
{
cout<<"I am base2"<<endl;
}
};
int main()
{
base x;
base1 x1;
base2 x2;
base *a[3]={&x,&x1,&x2};
for(int i=0;i<3;i++)
a[i]->i_am();
return 0;
}
运行测试:
3.编写程序,计算圆柱体,球体,正方体的表面积和体积。要求用抽象类实现。
#include <iostream>
using namespace std;
#define PI 3.14159
class Solid //定义抽象类
{
protected:
double r;
double s, v;
public:
virtual double S() = 0;
virtual double V() = 0;
Solid(double a, double b, double c)
{
r = a;
s = b;
v = c;
}
};
class Cube :public Solid
{
public:
Cube(double a, double b, double c) : Solid(a, b, c)
{
}
double S()
{
s = 6 * r * r;
return s;
}
double V()
{
v = r * r * r;
return v;
}
};
class Sphere :public Solid
{
public:
Sphere(double a, double b, double c) : Solid(a, b, c)
{
}
double S()
{
s = 4 * PI * r * r;
return s;
}
double V()
{
v = PI * r * r * r * 4 / 3;
return v;
}
};
class Cylinder :public Solid
{
protected:
double high;
public:
Cylinder(double h, double r, double a, double v) : Solid(r, a, v)
{
high = h;
}
double S()
{
s = 2 * PI * r * r + 2 * PI * r * high;
return s;
}
double V()
{
v = PI * r * r * high;
return v;
}
};
double source_area(Solid* p)
{
return p->S();
}
double volume(Solid* p)
{
return p->V();
}
int main()
{
char a;
double r0, h;
cout << "请输入正方体的边长:" ;
cin >> r0;
Cube cube(r0, 0, 0);
cout << "正方体的表面积为:" << source_area(&cube) << endl << "正方体的体积为:" << volume(&cube) << endl;
cout << "请输入球体的半径:";
cin >> r0;
Sphere sphere(r0, 0, 0);
cout << "球体的表面积为:" << source_area(&sphere) << endl << "球体的体积为:" << volume(&sphere) << endl;
cout << "请输入圆柱体的的底面半径:" ;
cin >> r0;
cout << "请输入圆柱体的的高:";
cin >> h;
Cylinder cylinder(r0, h, 0, 0);
cout << "球体的表面积为:" << source_area(&cylinder) << endl << "球体的体积为:" << volume(&cylinder) << endl;
return 0;
}
- 设计字符串类String,完成以下功能:
(1)使用+运算符实现两个字符串的连接功能。
(2)使用=运算符实现字符串的复制功能。
(3)使用 == 运算符判断两个字符串是否相等功能。
(4)使用[ ]运算符,可通过对象名和[ ]访问字符。
注意使用深复制方式。
#include<iostream>
#include<string>
#include<cstring>
#include<cstdio>
using namespace std;
class String
{
public:
String(){}
String operator+(const String &a);
String &operator=(const String &a);
int operator==(const String &a);
int Length(const String &a);
String(char x[])
{
s=new char[strlen(x)];
strcpy(s,x);
}
String(const String &str)
{
s=new char[strlen(str.s)+1];
strcpy(s,str.s);
}
char *Get_s()
{
return s;
}
private:
char *s;
};
String String::operator+(const String &a)
{
char *p=new char[strlen(a.s)+strlen(this->s)+1];
strcpy(p,this->s);
strcat(p,a.s);
return String (p);
}
String &String::operator=(const String &a)
{
s=new char[strlen(a.s)+1];
strcpy(s,a.s);
return *this;
}
int String::Length(const String &a)
{
return strlen(a.s);
}
int String::operator==(const String &a)
{
if(strcmp(s,a.s)==0)
return 0;
else
return 1;
}
int main()
{
String s1="helloFJUT", s2="helloworld", s3;
string s4="helloFJUT",s5="helloworld";
cout<<"s1=helloFJUT"<<" s2=helloworld"<<endl;
cout<<"s1的长度为:"<<s1.Length(s1)<<endl;
cout<<"s2的长度为:"<<s2.Length(s2)<<endl;
if((s1==s2)==0)
{
cout<<"s1,s2相等!"<<endl;
}
else
{
cout<<"s1,s2不相等!"<<endl;
cout<<"s1="<<s4<<endl;
cout<<"s2="<<s5<<endl;
}
return 0;
}
4.编写复数类Complxnum,将+,-,*,/运算符重载,实现对应功能。
#include<iostream>
using namespace std;
class Complexnum
{
public:
Complexnum(){real=0;imag=0;}//构造函数初始化
Complexnum(double r,double i){real=r;imag=i;}//带参数的构造函数
friend Complexnum operator+(Complexnum &c1,Complexnum &c2);//声明重载运算符+的函数作为友元函数
friend Complexnum operator-(Complexnum &c1,Complexnum &c2);
friend Complexnum operator*(Complexnum &c1,Complexnum &c2);
friend Complexnum operator/(Complexnum &c1,Complexnum &c2);
void display();
void get_value();
private:
double real;
double imag;
};
Complexnum operator+(Complexnum &c1,Complexnum &c2)
{
return Complexnum(c1.real+c2.real,c1.imag+c2.imag);
}
Complexnum operator-(Complexnum &c1,Complexnum &c2)
{
return Complexnum(c1.real-c2.real,c1.imag-c2.imag);
}
Complexnum operator*(Complexnum &c1,Complexnum &c2)
{
return Complexnum(c1.real*c2.real,c1.imag*c2.imag);
}
Complexnum operator/(Complexnum &c1,Complexnum &c2)
{
return Complexnum(c1.real/c2.real,c1.imag/c2.imag);
}
void Complexnum::get_value()
{
cin>>real>>imag;
}
void Complexnum::display()
{
cout<<"("<<real<<","<<imag<<"i)"<<endl;
}
int main()
{
Complexnum c1,c2,c3;
c1.get_value();
c2.get_value();
c3=c1+c2;
cout<<"c1=";c1.display();
cout<<"c2=";c2.display();
cout<<"c1+c2=";c3.display();
c3=c1-c2;
cout<<"c1-c2=";c3.display();
c3=c1*c2;
cout<<"c1*c2=";c3.display();
c3=c1/c2;
cout<<"c1/c2=";c3.display();
return 0;
}