一.雙目運算符重載
題目描述
編寫程序,按要求重載“+”、“-”運算符,實現兩個二維數組的相加和相減運算,要求第一個數組的值由構造函數設置(元素值依次爲:11 22 33 44 55 66),第二個數組的值由鍵盤輸入。
二組數組的行和列下標定義爲const常量,爲2行3列。參考類結構如下:
class MyArray
{
public:
MyArray();
MyArray(int a,int b,int c,int d,int e,int f);
void inputArray(); //鍵盤輸入數組的值
void display(); //顯示數組的值
MyArray operator+(MyArray &a); //加法運算符重載爲成員函數
friend MyArray operator-(MyArray &a, MyArray &b); // 減法運算符重載爲友元函數
private:
int var[Row][Col]; //Row、Col爲const常量
};
輸入描述
第二個數組各元素的值
輸出描述
輸出第一個數組的值
輸出第二個數組的值
兩個數組相加後的結果
兩個數組相減後的結果
輸入樣例
1 2 3
4 5 6
輸出樣例
Display object x
11 22 33
44 55 66
Display object y
1 2 3
4 5 6
Display object z=x+y
12 24 36
48 60 72
Display object z=x-y
10 20 30
40 50 60
#include<iostream>
using namespace std;
const int Row = 3;//弄反了
const int Col = 2;
class MyArray
{
public:
MyArray()
{
}
MyArray(int a, int b, int c, int d, int e, int f)//2行3列
{
var[0][0] = a;
var[0][1] = b;
var[0][2] = c;
var[1][0] = d;
var[1][1] = e;
var[1][2] = f;
}
void inputArray()
{
for (int i = 0; i < Col; i++)
{
for (int j = 0; j < Row; j++)
cin >> var[i][j];
}
}
void display()
{
for (int i = 0; i < Col; i++)
{
for (int j = 0; j < Row; j++)
cout << '\t' << var[i][j];
cout << endl;
}
}
MyArray operator+(MyArray &a)//每個元素相加
{
MyArray temp;
for (int i = 0; i < Col; i++)
{
for (int j = 0; j < Row; j++)
temp.var[i][j] = var[i][j] + a.var[i][j];
}
return temp;
}
friend MyArray operator-(MyArray &a, MyArray &b)
{
MyArray temp;
for (int i = 0; i < Col; i++)
{
for (int j = 0; j < Row; j++)
temp.var[i][j] = a.var[i][j] - b.var[i][j];
}
return temp;
}
private:
int var[Col][Row];
};
int main()
{
MyArray x(11, 22, 33, 44, 55, 66), y, z;
y.inputArray();
cout << "Display object x" << endl;
x.display();
cout << "Display object y" << endl;
y.display();
cout << "Display object z=x+y" << endl;
z = x + y;
z.display();
cout << "Display object z=x-y" << endl;
z = x - y;
z.display();
return 0;
}
二.Complex單目運算符重載
題目描述
編程實現Complex類前置++和後置—運算符的重載,Complex類的私有數據成員real和imag爲double型,要求:(1)前置自增++重載爲成員函數;(2)後置自減–重載爲友元函數;(3)定義print(),實現對象值的輸出,格式爲:(real,imag)
主函數中定義三個對象:c1,c2,c,對象c1通過構造函數直接指定複數的實部和虛部,分別爲2.5及3.7;對象c2的實部和虛部由鍵盤輸入。對象c的實部和虛部使用默認值,默認爲0。
執行按順序以下語句,然後輸出c和c1的值。
c = ++c1;
執行按順序以下語句,然後輸出c和c1的值。
c = c2–;
輸入描述
對象c2的初始值
輸出描述
執行c = ++c1; 後c與c1的值
執行c = c2–; 後c與c2的值
輸入樣例
4.2 6.5
輸出樣例
(3.5,4.7)
(3.5,4.7)
(4.2,6.5)
(3.2,5.5)
#include <iostream>
using namespace std;
class Complex{
public:
Complex(double r=0,double i=0)
{
real = r;
imag = i;
}
Complex operator++(){
++real;++imag;
return * this;
}
friend Complex operator--(Complex &op,int)
{
return Complex(op.real--,op.imag--);
}
void print(){cout<<"("<<real<<","<<imag<<")"<<endl;}
private:
double real,imag;
};
int main()
{
double r,i;
cin>>r>>i;
Complex c1(2.5,3.7),c2(r,i),c;
c = ++c1;
c.print();
c1.print();
c = c2--;
c.print();
c2.print();
return 0;
}
單目運算符重載與雙目運算符重載的區別
友元函數
雙目運算符有2個參數,單目運算符有1個參數
成員函數
雙目運算符有1個參數,單目運算符沒有參數,就自己++或者–就行
單目運算符的重載分前置和後置
後置在後面就加一個假參數int即可
friend Complex operator--(Complex &op,int)
{
return Complex(op.real--,op.imag--);
}
成員函數的返回值
要返回this指針
Complex operator++(){
++real;++imag;
return * this;
三.抽象類與虛函數
題目描述
編寫程序,定義抽象類Container,有double型保護數據成員radius,由它公有派生出3個類:Cube(立方體)、Sphere(球體)、Cylinder(圓柱體),派生類中增加的數據成員也爲double型。
抽象類Container的結構如下:
class Container //聲明抽象類
{
public:
Container(double radius); //抽象類的構造函數
virtual double calSurfaceArea()=0; //純虛函數
virtual double calVolume()=0; //純虛函數
protected:
double radius;
};
主函數中定義抽象類的指針,再定義Cube(立方體)、Sphere(球體)、Cylinder(圓柱體)的對象各一個,各對象的初始值均由鍵盤輸入。應用C++的多態性,將各派生類對象的地址賦給基類指針,通過指針計算各對象的表面積和體積並輸出。
π的值:3.1415926
輸入描述
立方體的邊長
球體的半徑
圓柱體的半徑和高
輸出描述
立方體的表面積和體積
球體的表面積和體積
圓柱體的表面積和體積
輸入樣例
5
5
5 6
輸出樣例
立方體的表面積:150,體積:125
球體的表面積:314.159,體積:523.599
圓柱體的表面積:345.575,體積:471.239
#include<iostream>
using namespace std;
const double PI=3.1415926;
class Container //聲明抽象類
{
public:
Container(double radius)
{this->radius=radius;}//抽象類的構造函數
virtual double calSurfaceArea()=0; //純虛函數
virtual double calVolume()=0; //純虛函數
protected:
double radius;
};
class Cube:public Container
{
public:
Cube(double radius):Container(radius)
{}
double calSurfaceArea()
{return radius*radius*6;}
double calVolume()
{return radius*radius*radius;}
};
class Sphere:public Container
{
public:
Sphere(double radius):Container(radius)
{}
double calSurfaceArea()
{return radius*4*PI*radius;}
double calVolume()
{return radius*radius*radius*PI*4/3;}
};
class Cylinder:public Container
{
public:
Cylinder(double radius,double height):Container(radius)
{this->height=height;}
double calSurfaceArea()
{return PI*2*radius*radius+PI*2*radius*height;}
double calVolume()
{return PI*radius*radius*height;}
private:
double height;
};
int main()
{
Container *p;
double a,b,c1,c2;
cin>>a>>b>>c1>>c2;
Cube A(a);
Sphere B(b);
Cylinder C(c1,c2);
p=&A;
cout<<"立方體的表面積:"<<p->calSurfaceArea()<<",體積:"<<p->calVolume()<<endl;
p=&B;
cout<<"球體的表面積:"<<p->calSurfaceArea()<<",體積:"<<p->calVolume()<<endl;
p=&C;
cout<<"圓柱體的表面積:"<<p->calSurfaceArea()<<",體積:"<<p->calVolume()<<endl;
return 0;
}