C++类与对象第四章习题

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


提示:以下是本篇文章正文内容,下面案例可供参考

4-8 定义一个Dog类,包含age、weight等属性,以及对这些属性操作的方法。实现并测试这个类

#include<iostream>

using namespace std;


class Dog{
	public:
		Dog(int initialAge = 0, int initialWeight = 5); //定义 构造函数  
		~Dog();   //定义 析构函数 
		int getAge(){
			return age;
		}
		int setAge(int nowAge){
			age = nowAge;
		}
		int getWeight(){
			return weight;
		}
		int setWeight(int nowWeight){
			weight = nowWeight;
		}
	private:
		int age, weight;
}; 
Dog:: Dog(int initialAge, int initialWeight){ // 构造函数 
	age = initialAge;
	weight = initialWeight;
}
Dog::~Dog(){ //析构函数,但是在本题不做任何工作 
	
}
int main(){
	Dog Jack(2,10); // Dog是类 Jack是对象,创建对象的同时调用构造函数进行初始化 
	cout << "Jack is a dog who is ";
	cout << Jack.getAge() << " years old and " << Jack.getWeight() << " pounds weight" << endl;
	Jack.setAge(7);  // 通过对象调用类的公有成员函数 
	Jack.setWeight(20);
	cout << "Now Jack is " ;
	cout <<  Jack.getAge() << " years old and " << Jack.getWeight() << " pounds weight" << endl;
	return 0;
}

运行结果:

Jack is a dog who is 2 years old and 10 pounds weight
Now Jack is 7 years old and 20 pounds weight

Dog(int initialAge = 0, int initialWeight = 5);这行代码是在类中声明了构造函数,并且形参有默认值分别为0、5,那么后面在主函数中,能不能不传入值,因为反正已经有默认值, 那我就用你给的这个默认值,原本是:Dog Jack(2,10);,所以运行结束第一行是2 years old and 10 pounds weight
那我把Dog Jack(2,10);改成Dog Jack();是不是输出就是0 years old and 5 pounds weight。NO!!不是的,这样编译器会报错。
因为C++会把Dog Jack();当成是你定义了一个Jack函数,返回Dog类型,(非常的离谱)而不是定义一个Jack对象。那正确的写法是什么呢?就是:Dog Jack;或者Dog Jack{};建议用后者,后者是C++11的写法,然后你再运行输出就是0 years old and 5 pounds weight

4-9 设计并测试一个名为Rectangle的矩形类,其属性为矩形的左下角与右上角两个点的坐标,根据坐标能计算矩形的面积。

#include<iostream>

using namespace std;

class Rectangle{
	public:
		Rectangle(int initialTop, int initialLeft, int initialBottom, int initialRight);
		~Rectangle();
		int area(int top, int left, int bottom, int right){
			return (top-bottom) * (left-right);
		}
	private:
		int top, left, bottom, right;
};
Rectangle::Rectangle(int initialTop, int initialLeft, int initialBottom, int initialRight){
	top = initialTop;
	left = initialLeft;
	bottom = initialBottom;
	right = initialRight;
}
Rectangle::~Rectangle(){
}
int main(){
	Rectangle juxingA(0,0,0,0);
	cout << juxingA.area(3,2,0,0) << endl;
	return 0;
}

然后这个在写的时候也踩雷了,上一题刚学会了在主函数中我能写成Dog Jack;那我这个题我就写Rectangle juxingA;就是我不传入参数嘛,好家伙,直接给我报错。
因为你前面定义了一个 有参构造函数啊!! 而且最重要的是,你在 定义的时候没有给参数默认值, Rectangle(int initialTop, int initialLeft, int initialBottom, int initialRight);,所以它必须传4个参数,才能调用!!!

Rectangle::Rectangle(int initialTop, int initialLeft, int initialBottom, int initialRight){
	top = initialTop;
	left = initialLeft;
	bottom = initialBottom;
	right = initialRight;
}

在 C++ 里更推荐用 初始化列表 来写构造函数:(但是感觉上面的更好看嘛)

//初始化列表的写法,这个效率更高
Rectangle::Rectangle(int initialTop, int initialLeft, int initialBottom, int initialRight)
    : top(initialTop), left(initialLeft), bottom(initialBottom), right(initialRight) {}

4-10设计一个用于人事管理的“人员”类。由于考虑到通用性,这里只抽象出所有类型人员都具有的属性:编号、性别、出生日期、身份证号等。其中“出生日期”声明为一个“日期”类内嵌子对象。用成员函数实现对人员信息的录入和显示。要求包括:构造函数和析构函数、复制构造函数、内联成员函数、带默认形参值的成员函数、类的组合。

#include<iostream>
#include<string>

using namespace std;

class Date{
	private:
		int year;
		int month;
		int day;
	public:
		Date(int y = 2000, int m = 1, int d = 1){
			year = y;
			month = m;
			day = d;
		}
		~Date(){ //析构函数 
		}
		void showDate(){
			cout << year << "." << month << "." << day; 
		}
		void inputDate(){
			cout << "请输入出生日期:";
			cin >> year >> month >> day;
		}
}; 
class Person{
	private:
		int id;
		string gender;
		string idNumber;
		Date birthday;
	public:
		Person(int i = 0, string g = "未知", string idNum = "无", Date b = Date()){
			id = i;
			gender = g;
			idNumber = idNum;
			birthday = b;
		}
		// 复制构造函数
		Person(const Person& other) {
		    id = other.id;
		    gender = other.gender;
		    idNumber = other.idNumber;
		    birthday = other.birthday; // Date类也会调用它的复制构造函数
		}

		~Person(){
		}
		void inputInfo(){
			cout <<"请输入人员编号";
			cin >> id;
			cout << "请输入性别";
			cin >> gender;
			cout << "请输入身份证号";
			cin >> idNumber;
			birthday.inputDate(); 
		}
		void showInfo(){
			cout << "编号" << id << endl;
			cout << "性别" << gender << endl;
			cout << "身份证号" << idNumber << endl;
			cout << "出生日期:";
			birthday.showDate();
			cout << endl;
		}
		int getId() const { return id; }

};
int main(){
	cout << "测试人员" << endl;
	Person p1(1001, "男","123456789",Date(1999,10,1));
	p1.showInfo();
	
	cout << "输入新人员" << endl;
	Person p2;
	p2.inputInfo();
	p2.showInfo();
	
	cout << "使用复制构造函数" << endl;
	Person p3(p1);  //使用复制构造函数
	p3.showInfo();
	
	return 0; 
}

4-11 定义并实现一个矩形类,有长宽两个属性,由成员函数计算矩形的面积。

#include<iostream>

using namespace std;

class Rectangle{
	public:
		Rectangle(float l, float w){ // 构造函数 
			length = l;
			width = w;
		}
		~Rectangle(){  //析构函数 
		} 
		float getArea(){  //这里不需要在传入参数了,直接用成员变量 
			return length*width;
		}
	private:
		float length;
		float width;
};

int main(){
	float l;
	cout << "清输入长度:";
	cin >> l;
	float w;
	cout << "请输入宽度:";
	cin >> w;
	Rectangle r(l, w);
	cout << r.getArea() << endl;
	return 0;
	
}

4-12 定义一个DataType(数据类型)类,能处理包含字符型、整数型、浮点型3种类型的数据,给出其构造函数。

(这题目要求我都没整明白,啥叫能处理啊)

#include<iostream>

using namespace std;

class DataType{
	private:
		enum vartype{character,integer,floatint_point};
		vartype type;
		
		char c;
		int i;
		float f;
		
	public:
		DataType(char ch){ //这里写了三个构造函数的形式,当然也可以写成普通函数的形式,只不过在主函数中调用会更麻烦一点 
			type = character;
			c = ch;
		}
		DataType(int ii){
			type = integer;
			i = ii;
		}
		DataType(float ff){
			type = floatint_point;
			f = ff;
		}
		void print(){
			if(type == character) cout << "字符型:" << c << endl;
			else if (type == integer) cout << "整数型:" << i << endl;
			else if (type == floatint_point) cout << "浮点型:" << f << endl;
		}
}; 
int main() {
    DataType a('A');
    DataType b(123);
    DataType c(3.14f);

    a.print();  // char: A
    b.print();  // int: 123
    c.print();  // float: 3.14
}

4-13定义一个Circle类,有数据成员radius(半径)、成员函数getArea(),计算圆的面积,构造一个Circle的对象进行测试。

#include<iostream>

using namespace std;

class Circle{
	public:
		Circle(float r) : radius(r) {}
		~Circle(){}
		void getArea(){
			cout << 3.14*radius*radius << endl;
		}
	private:
		float radius;
};
int main(){
	float radius;
	cout << "请输入圆的半径:";
	cin >> radius;
	Circle a(radius);
	a.getArea();
	return 0;
}

4-14定义一个Tree(树)类,有成员ages(年龄),成员函数grow(int years)对ages加上years,age()显示tree对象的ages的值

#include<iostream>

using namespace std;

class Tree{
	public:
		Tree(int a = 0);
		~Tree();
		void grow(int years);
		void age();
	private:
		int ages;
};
Tree::Tree(int a){
	ages = a;
}
Tree::~Tree(){
	age();
}
void Tree::grow(int years){
	ages += years;
}
void Tree::age(){
	cout << ages << endl;
}
int main(){
	Tree a(2);
	a.age(); 
	a.grow(3);
	return 0;
}

这个代码运行会显示2和5

为什么?明明主函数中a.grow(3);之后没有调用age函数来输出,可是最后还是输出了5

调用a.grow(3),将ages增加3,此时ages变为5。

main函数结束,对象a的生命周期结束,调用析构函数~Tree()。

在析构函数中,调用了age()函数,输出ages的值5。


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值