第4章  类与对象

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

#include <iostream>
using namespace std;
class Rectangle{
	private:
		double x,y;
	public:
		void setPoint(){
			cin>>x>>y;
		}
		double area(const Rectangle &t ){
			return (x-t.x)*(y-t.y);
		}
};
int main() {
   Rectangle a,b;
   cout<<"请输入左下角的横坐标、纵坐标:"; 
   a.setPoint();  // 这里读入左下角的横坐标、纵坐标
   cout<<"请输入右上角的横坐标、纵坐标:";
   b.setPoint();  // 这里读入右上角的横坐标、纵坐标
   
   cout << b.area(a) << endl;

   return 0;
}

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

#include <iostream>
using namespace std;

class date{
	private:
		int year, month, day;
	public:
		date() {                //这里初始化为2000年1月1日 
			year = 2000;
			month = 1;
			day = 1;
	    }
		date(const date& d){      //拷贝函数 
			year = d.year; month = d.month; day = d.day;
		}
		~date() {}
		
		void setdate(){
			cout << "请输入日期年、月、日:" << endl;
			cin >> year >> month >> day;
		}
		void alterdate(int x = 0, int y = 0, int z = 0){
			year = x;
			month = y;
			day = z;
		}
		void display(){
			cout << "出生日期:" << year << "年" << month << "月" << day << "日" << endl;
		}
};

class people {
	private:
		int num;
		string sex;
		date birthday;
		string ID;
	public:
		people(date b):birthday(b) {
			int n = 0;
			string sx,I;
			num = n; sex = sx; ID = I;
		}
		people(const people& p):birthday(p.birthday) { 
			num = p.num; sex = p.sex; ID = p.ID; 
		}
		~people() {};
		
		void setpeople(); 
		void display();          
};
void people::setpeople() 
{
	int n; string d; date a; string I;
	a.setdate();
	cout << "请输入人员编号" << endl;
	cin >> n;
	cout << "请输入人员性别" << endl;
	cin >> d;
	cout << "请输入身份证号" << endl;
	cin >> I;
	birthday = a;
	num = n;
	sex = d;
	ID = I;
}
inline void people::display() //内联函数,免去函数调用的复杂操作
{
	cout<<endl; 
	cout << "编号:"<< num <<endl<< "性别:"<< sex <<endl<<"身份证号:" << ID <<endl;
	birthday.display();
}
int main() {
	
	date b;
	people p(b);
	p.setpeople();
	p.display();
	
	people q(p);    //复制拷贝 
	q.display();
	
	return 0;
}

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

#include<iostream>
using namespace std;

class Rectangle{
	private:
		float leng,wide;
	public:
		Rectangle(float l,float w){
			leng=l;
			wide=w;
		}
		~Rectangle(){
		}
		float getArea(){
			return leng*wide;
		}
};

int main()
{
	float x,y;
	cout<<"请输入矩形的长度和宽度:";
	cin>>x>>y;
	Rectangle r(x,y);
	cout<<"矩形面积为:"<<r.getArea()<<endl; 
	return 0;
}

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

#include<iostream>
using namespace std;

class DataType{
	private:
	    int a;
	    float b;
		char c;
		
	public:
		DataType(int i,float j,char k){
			a=i;b=j;c=k;
		}
		void print(){
			cout<<a<<" "<<b<<" "<<c<<" "<<endl; 
		}
};

int main()
{
	DataType d( 3, 5.6, 'a');
	d.print();
	return 0;
}

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

#include<iostream>
using namespace std;

class Circle{
	private:
		float R; 
	public:
		Circle(float r){
			R=r;
		}
		~Circle(){
		} 
		float getArea(){
			return 3.14*R*R;
		}
};

int main()
{
	float r;
	cout<<"请输入圆的半径:";
	cin>>r;
	Circle c(r);
	cout<<"该圆的面积为:"<<c.getArea()<<endl; 
	return 0;
}

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

#include<iostream>
using namespace std;

class tree
{
	private:
		int ages; 
	public:
		tree(){ ages = 0;}
		void grow(int years);
		void age();
};

void tree::grow(int years)
{
	ages+=years;
}

void tree::age()
{
	cout << ages <<endl;
}

int main()
{
	tree a;
	int i;
	cout << "Please enter the tree age:" << endl;
	cin >> i;
	a.grow(i);
	cout << "The age of the tree is:" << endl;
	a.age();
	return 0;
}

4-19编写一个名为CPU的类,描述一个CPU的以下信息:时钟频率,最大不会超过3000MHZ;字长,可以是32位或是64位;核数,可以是单核、双核或四核;是否支持超线程。各项信息要求使用位域来表示。通过输出sizeof(CPU)来观察该类所占的字节数。

#include <iostream>
using namespace std;

class CPU{
	public:
		CPU(int i, int j, string m, bool n)      //构造函数
		{
			Hz = i;
			zichang = j;
			he = m;
			xiancheng = n;
		}
		void show()     //打印
		{
			cout << "时钟频率:"<< Hz <<"MHz"<< endl;
			cout << "字长:"<< zichang <<"位"<< endl;
			cout << "核数:"<<he << endl;
			cout << "是否支持超线程:";
			if (xiancheng == 1)
				cout << "true" << endl;
			else
				cout << "false" << endl;
		} 
	private:
		int Hz:32;
		int zichang:16;
		string he;
		bool xiancheng:8;
};

int main()
{
	CPU  c(2000, 64, "四核", false);
	c.show();
	cout << "类所占的字节数:" << sizeof(CPU) << endl;
	return 0;
}

4-20定义一个负数类Complex,使得下面的代码能够工作。

Complex  c1(3,5);              //用复数3+5i初始化c1

Complex  c2=4.5;              //用实数4.5初始化c2

C1.add(c2);                    //将c1与C2相加,结果保存在c1中

C1.show();                   //将c1输出(这时的结果应该是7.5+5i);

#include <iostream>
using namespace std;
 
class Complex{
private:
    float re, im;
public:
    Complex(float r, float i=0){
        re=r;
        im=i;
    }
    void add(Complex com){
        re=re+com.re;
        im=im+com.im;
    }
    void show(){
        cout<<re<<"+"<<im<<"i"<<endl;
    }
    
};
 
int main()
{
    Complex c1(3,5);   
 
    Complex c2=4.5;     
    
    c1.show();
 
    c1.add(c2);   
 
    c1.show(); 
    
    return 0;
}

最后的最后,再来分享一些小干货:

  1. 在前面的学习中,我们可以发现在定义变量时赋初值,可以有两种方式。比如要给int型变量赋值为5,可以是 a=5  或者是  a(5)
  2. 函数在定义时可以预先声明默认的形参值,调用时如果事先给出实参,则按实参的值,否则按预先声明的形参默认值,这就是带默认值的函数。比如上面最后一道题的构造函数Complex(float r, float i=0) ,i值默认为0。
  3. 但要注意是,有默认值的形参必须在无默认值形参后面,即Complex(float r=0,float i) 就不行。因为假设你不想输入r值,想输入i值,那么你输入的那一个值编译器会从左到右识别形参,然后就赋给了第一个值r了,这样就不符合我们的预期结果。所以设置默认形参的值时,应该从右到左进行设置。

本专栏为本人大二C++课程的习题作业和一些学习经验的分享,供大家参考学习。如有侵权请立即与我联系,我将及时处理。


参考书籍为:C++语言程序设计 第五版 -清华大学出版社- 郑莉,董渊、C++语言程序设计 第五版 -清华大学出版社- 郑莉,董渊(学生用书)

编译环境:Visual Studio 2019、Dev-C++

欢迎关注我的微信公众号,分享一些有趣的知识:程序猿的茶水间

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐