- 综合改错(老师发的题目排版可能有问题):
#include <iostream>
#include<stdlib.h>
class CComplex
{
public:
CComplex(double r = 0, double i = 0)
{
real = r;
imag = i;
}
int operator int()
{
return (int)real;
}
void Display(void)
{
cout << "(" << real << "," << imag << ")" << endl;
}
protected:
double real;
double imag;
};
class CVector
{
public:
CVector(CComplex& obj1, CComplex& obj2, CComplex& obj3, CComplex& obj4)
{
objArray[0] = obj1;
objArray[1] = obj2;
objArray[2] = obj3;
objArray[3] = obj4;
}
friend CComplex& operator[](CVector obj, int n);
private:
CComplex objArray[4];
};
CComplex& operator[](CVector obj, int n)
{
if (n < 0 || n>3)
{
cout << "Out of range!" << endl;
exit(0);
}
return obj.objArray[n];
}
void main()
{
CComplex c1(1.1, 1.1);
CComplex c2(2.2, 2.2);
CComplex c3(3.3, 3.3);
CComplex c4(4.4, 4.4);
CVector v(c1, c2, c3, c4);
v[0].Display();
v[1].Display();
v[2].Display();
v[3].Display();
v[0] = 5.5;//1
v[1] = CComplex(6.6);//2
v[2] = int(CComplex(7.7));//3
v[3] = int(CComplex(8.8, 9.9));//4
v[0] = Display();
v[1] = Display();
v[2] = Display();
v[3] = Display();
}
问题一:上述程序存在两大错误,在不修改主函数和程序原意的前提下,改正该程序中存在的错误。
改正后代码:
#include <iostream>
#include"stdlib.h"
using namespace std;//std流运算符
class CComplex
{
public:
CComplex(double r = 0, double i = 0)
{
real = r;
imag = i;
}
operator int() //第一大错误:删除第一个int,operator用于类型转换函数时:类型转换函数由operator(前置)修饰,函数名称为目标类型名
{
return (int) real;
}
void Display(void)
{
cout << "(" << real << "," << imag << ")" << endl;
}
protected:
double real;
double imag;
};
class CVector
{
public:
CVector(CComplex& obj1, CComplex& obj2, CComplex& obj3, CComplex& obj4)
{
objArray[0] = obj1;
objArray[1] = obj2;
objArray[2] = obj3;
objArray[3] = obj4;
}
//原,删去friend CComplex& operator[](CVector obj, int n);
CComplex & operator [] (int n);//下标运算符[]是一个双目运算符,只能重载为成员函数,删去形参定义部分及friend关键字
private:
CComplex objArray[4];
};
//原,删去CComplex & operator [](CVector obj, int n)
CComplex & CVector::operator[](int n)//第二大错误:下标运算符[]是一个双目运算符,只能重载为成员函数,对应的运算符重载函数形式为: type1 & operator[](type2)
//其中,type1为数组类型,type2为下标类型。
//C++预定义对type1没有限制,但要求type2必须是int型,数组需要先声明后使用,若type2是其它类型,就需要对下标运算符重载。
{
if (n < 0 || n>3)
{
cout << "Out of range!" << endl;
exit(0);
}
return objArray[n];//删除obj.
}
int main()//main的返回类型应为int而非void
{
CComplex c1(1.1, 1.1);
CComplex c2(2.2, 2.2);
CComplex c3(3.3, 3.3);
CComplex c4(4.4, 4.4);
CVector v(c1, c2, c3, c4);
v[0].Display();
v[1].Display();
v[2].Display();
v[3].Display();
v[0] = 5.5;
v[1] = CComplex(6.6);
v[2] = int(CComplex(7.7));
v[3] = int(CComplex(8.8, 9.9));
v[0] .Display();
v[1] . Display();
v[2] . Display();
v[3] . Display();
}
问题二:①处的转换属于显式转换还是隐式转换,并解释该转换过程。
隐式转换…
问题三:②处的转换属于显式转换还是隐式转换,并解释该转换过程。
显式转换…
问题四:解释③处的转换过程。
问题五:解释④处的转换过程。
- 编写一个程序计算三角形、正方形和圆形的面积,要求抽象出一个基类base,在其中说明一个虚函数,用来求面积,并利用单接口,多实现版本设计各图形面积的方法。
#include <iostream>
using namespace std;
class base
{
virtual void area() = 0;//虚函数
};
class triangle :public base
{
public:
triangle()
{
cout << "请输入三角形的底" << endl;
cin >> hemline;
cout << "请输入三角形的高" << endl;
cin >> height;
}
void area()
{
cout << "三角形的面积是" << hemline * height / 2 << endl;
}
private:
double hemline;
double height;
};
class square :public base
{
public:
square()
{
cout << "请输入正方形的边长" << endl;
cin >> length;
}
void area()
{
cout << "正方形的面积是" << length * length / 2 << endl;
}
private:
double length;
};
class circular:public base
{
public:
circular()
{
cout << "请输入圆形的半径"<<endl;
cin >> radius;
cout << "请输入pi的值" << endl;
cin >> pi;
}
void area()
{
cout << "圆形的面积是" << radius * radius * pi;
}
private:
double radius;
double pi;
};
int main()
{
triangle tri;
tri.area();
square squ;
squ.area();
circular cir;
cir.area();
}
- 设计一个汽车类Motor,该类具有可载人数、轮胎数、马力数、生产厂家和车主五个数据成员,根据Motor类派生出Car类、Bus类和Truck类。其中Bus类除继承基类的数据成员外,还具有表示车厢节数的数据成员Number;Truck类除继承基类的数据成员之外,还具有表示载重量的数据成员Weight。每个类都具有成员函数Display,用于输出各类对象的相关信息。要求设计一个统一的显示相关信息的全局函数ShowInfo在主函数中调用,主函数中不直接调用类里面的Display函数。
#include<iostream>
#include<string>
using namespace std;
class Motor
{
public:
Motor(int pas , int wheel , int push1 , string madeby1, string owner1) { passengers = pas, wheels = wheel, pushs = push1, madeby = madeby1, owner = owner1; }
virtual void Display()//void Display()
{
cout << "可载人数是" << passengers << "人" << endl;
cout << "轮胎数是" << wheels << "个" << endl;
cout << "马力数是" << pushs << endl;
cout << "生产厂家是" << madeby << endl;
cout << "车主是" << owner << endl;
cout << endl;
}
protected:
int passengers;
int wheels;
int pushs;
string madeby;
string owner;
};
class Car :public Motor
{
public:
Car(int pas, int wheel, int push1, string madeby1, string owner1) :Motor(pas, wheel, push1, madeby1, owner1)
{
passengers = pas, wheels = wheel, pushs = push1, madeby = madeby1, owner = owner1;
}
void Display()
{
Motor::Display();
cout << endl;
}
private:
int passengers;
int wheels;
int pushs;
string madeby;
string owner;
};
class Bus :public Motor
{
public:
Bus(int pas, int wheel, int push1, string madeby1, string owner1,int num) :Motor(pas, wheel, push1, madeby1, owner1)
{
Number = num;
}
void Display()
{
cout << "Bus的车厢节数是" << Number << endl;
Motor::Display();
}
private:
int Number;
};
class Truck :public Motor
{
public:
Truck(int pas, int wheel, int push1, string madeby1, string owner1, double wei) : Motor(pas, wheel, push1, madeby1, owner1)
{
Weight = wei;
}
void Display()
{
cout << "Truck的载重量是" << Weight << endl;
Motor::Display();
}
private:
double Weight;
};
/*int main()
{
Motor motor1(50, 30, 20, "afib", "sfbisbug");
motor1.Display();
Car car1(50, 30, 20, "afib", "sfbisbug");
car1.Display();
Bus bus1(40, 20, 10, "afiubf", "abfb", 50);
bus1.Display();
Truck truck1(10, 20, 30, "afigf", "afnkb", 20.0);
truck1.Display();
}*/
void ShowInfo(Motor* a) {
a->Display();
}
void main()
{
Car car1(5, 4, 1000, "TK", "TK_DS");
Bus bus1(34, 6, 8000, "TK", "TK_DS",3);
Truck truck1(2, 12, 11000, "TK", "TK_DS",8);
Motor* pr = &car1;
ShowInfo(pr);
pr = &bus1;
ShowInfo(pr);
pr = &truck1;
ShowInfo(pr);
}