又是一次C++作業(看時間,同班尤慎)

  • 綜合改錯(老師發的題目排版可能有問題):
#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);
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章