一次C++作業與總結

改錯1

#include <iostream>
#include<string>
using namespace std;

class CBase1
{
public:
    CBase1() :a(a)//CBase1不存在默認構造函數,應刪去括號內的int a,使系統生成默認構造函數
    {
        cout << "base1 structure..." << endl;
    }
    ~CBase1()
    {
        cout << "base1 destructure ..." << endl;
    }
    void print()
    {
        cout << "a=" << a << endl;
    }
protected:
    int a;
};
class CBase2
{
public:
    CBase2() :b(b)//CBase2不存在默認構造函數,應刪去括號內的int b,使系統生成默認構造函數
    {
        cout << "base2 structure..." << endl;
    }
    ~CBase2()
    {
        cout << "base2 destructure..." << endl;
    }
    void print()
    {
        cout << "b=" << b << endl;
    }
protected:
    int b;
};
class CDerive :public CBase1, public CBase2
{
public:
    CDerive()
    {
        cout << "derive structure..." << endl;
    }
    ~CDerive()
    {
        cout << "derive destructure..." << endl;
    }
    void print()
    {
        CBase1::print();
        CBase2::print();
        b1.print();
        b2.print();
        cout << "c=" << c << endl;
    }
private:
    CBase1 b1;
    CBase2 b2;
    int c;
};

int main()//main 的返回類型應爲int
{
    CDerive d;
    d.print();//調用函數時應加上括號
}

改錯2

#include<iostream>
#include<string>
using namespace std;
class CBase
{
public:
	CBase(int a) :a(a)
	{}
	int a;
};
class CDerive1 : public CBase
{
public:
	CDerive1(int a) :CBase(a)
	{}
};
class CDerive2 : public CBase
{
public:
	CDerive2(int a) : CBase(a)
	{}
};
class CDerive :public CDerive1, public CDerive2
{
public:
	CDerive(int a, int b) :CDerive1(a), CDerive2(b)
	{}
};
int main()
{
	CDerive d(1, 2);
	cout << d.CDerive1::a << endl;
	cout << d.CDerive2::a << endl;
}

建立普通的基類building,用來存儲一座樓房的層數、房間數以及它的總平方數。建立派生類house,繼承building,並存儲臥室與浴室的數量,另外,建立派生類office,繼承building,並存儲滅火器與電話的數目。設計一主函數來測試以上類的用法。

#include <iostream>
#include<string>
using namespace std;

class building
{
public:
	building(int f, int r, double a) { floors = f, rooms = r, area = a; }
	/*更改處:調用基類內的參數時應同名函數:到相應參數
	void Building(int f, int r, int a)
	{
		floors = f;
		rooms = r;
		area = a;
	}*/
	void show()
	{
		cout << "建築層數" << floors << endl;
		cout << "建築房間數" << rooms << endl;
		cout << "建築總平方米數" << area << endl;
	}
protected:
	int floors;
	int rooms;
	double area;
};

class house :public building
{
public:
	house(int f, int r, double a, int bed, int bash) :building(f, r, a)
	{
		bedrooms = bed;
		bashrooms = bash;
	}
	void show()
	{
		cout << "建築層數" << floors << endl;
		cout << "建築房間數" << rooms << endl;
		cout << "建築總平方米數" << area << endl;
		cout << "臥室數量" << bedrooms << endl;
		cout << "浴室數量" << bashrooms << endl;
	}
private:
	int bedrooms;
	int bashrooms;
};

class office :public building
{
public:
	office(int f, int r, double a, int fire, int phone) :building(f, r, a)
	{
		antifire = fire;
		phones = phone;
	}
	void show()
	{
		cout << "建築層數" << floors << endl;
		cout << "建築房間數" << rooms << endl;
		cout << "建築總平方米數" << area << endl;
		cout << "滅火器數量" << antifire << endl;
		cout << "電話數量" << phones << endl;
	}
private:
	int antifire;
	int phones;
};

int main()
{
    building b1(8, 14, 2000.0);
	b1.show();
	cout << endl;
	house h1(10, 20, 5000.0, 10, 20);
	h1.show();
	cout << endl;
	office o1(12, 28, 8000.0, 40,100);
	o1.show();@[TOC](這裏寫自定義目錄標題)
}
  • 按照下圖的類層次結構編寫程序,定義屬於score的對象c1以及類teacher的對象t1,分別輸入各個數據成員的值後再顯示出這些數據。
    在這裏插入圖片描述
#include<iostream>
#include<string>
using namespace std;

class person
{
public:
	person(string n, string i) { name = n, id = i; }
	void printper()
	{
		cout << "姓名:" << name << endl;
		cout << "身份證號碼:" << id << endl;
	}
protected:
	string name;
	string id;
};
class teacher :public person
{
public:
	teacher(string name ,string id,string deg, string dep) :person(name,id){ degree = deg, department = dep; }
	void printtea()
	{
		printper();//不能用void的形式
		cout << "學位:" << degree << endl;
		cout << "部門:" << department << endl;
	}
private:
	string degree;
	string department;
};
class student :public person
{
public:
	student(string n, string i, int old1, string sno1) :person(n, i) { old = old1, sno = sno1; }
	void printstu()
	{
		printper();
		cout << "學生年齡:" << old << endl;
		cout << "學生學號:" << sno << endl;
	}
protected:
	int old;
	string sno;
};
class stud
{
public:
	stud(string addr, string tel) { address = addr, tellphone = tel; }
	void printstud()
	{
		cout << "學生地址:" << address << endl;
		cout << "學生電話號碼:" << tellphone << endl;
	}
protected:
	string address;
	string tellphone;
};
class score :public stud, public student
{
public:
	score(string name,string id,int old,string sno,string address,string tellphone,float ma, float eng) : student( name,id,old,sno),stud(address,tellphone){ math = ma, english = eng; }
	void printsco()
	{
		printstu();
		printstud();
		cout << "數學成績:" << math << endl;
		cout << "英語成績:" << english << endl;
	}
private:
	float math;
	float english;
};
int main()
{
	score c1( "張三","100100100101010011",19, "2019010101","北京海淀區","1234567890123", 100.0, 100.0);
	c1.printsco();
	cout << endl;
	teacher t1("李四", "100100002222222222", "博士生導師", "法學院");
	t1.printtea();

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章