类和对象

本文通过多个实例展示了C++中类与对象的应用,包括数组最大值查找、时间显示、长方体体积计算等,涉及构造函数、成员函数及对象指针等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

例1:找出一个整型数组中的元素的最大值。(用类的方法来解决)

#include<iostream>
using namespace std;

class Array_max
{
public:
	void set_value();
	void max_value();
	void show_value();
private:
	int array[10];
	int max;
};
void Array_max::set_value()
{
	int i;
	for(i=0;i<10;i++)
		cin>>array[i];
}
void Array_max::max_value()
{
	int i;
	max=array[0];
	for(i=1;i<10;i++)
		if(max<array[i]) 
			max=array[i];
}
void Array_max::show_value()
{
	cout<<"max="<<max<<endl;
}

int main()
{
	Array_max a;
	a.set_value();
	a.max_value();
	a.show_value();


	return 0;
}

===================================================================================================================================

例2:显示时间(用类的方法来解决)

#include<iostream>
using namespace std;

class Time
{
public:
	void set_time();
	void show_time();
private:
	int hour;
	int minute;
	int second;
};
void Time::set_time()
{
	cin>>hour;
	cin>>minute;
	cin>>second;
}
void Time::show_time()
{
	cout<<hour<<":"<<minute<<":"<<second<<endl;
}

int main()
{
	Time t;
	t.set_time();
	t.show_time();
	return 0;
}


================================================================================================================================

例3、需要求3个长方柱的体积,请编写一个基于对象的程序。数据成员包括length(长)、width(宽)、height(高),要求用成员函数实现以下功能:  25
(1)由键盘分别输入3个长方柱的长、宽、高;
(2)计算长方柱的体积;
(3)输出3个长方柱的体积。

#include<iostream>
using namespace std;

class Box
{
public:
	void set();
	double volume();
	void show();
private:
	double length;
	double width;
	double height;
};
void Box::set()
{
	cout<<"Please input length,width,height"<<endl;
	cin>>length;
	cin>>width;
	cin>>height;
}
double Box::volume()
{
	return (length*width*height);
}
void Box::show()
{
	cout<<"volume="<<volume()<<endl;
}

int main()
{
	Box box1,box2,box3;
	
	box1.set();
	cout<<"The volume of box1 is";
	box1.show();

	box2.set();
	cout<<"The volume of box2 is";
	box2.show();
	
	box3.set();
	cout<<"The volume of box3 is";
	box3.show();

	return 0;
}


==================================================================================================================================

例4、在例2基础上定义构造成员函数。

【代码1】

#include<iostream>
using namespace std;

class Time
{
public:
	Time()
	{
		hour=0;
		minute=0;
		second=0;
	}
	void set_time();
	void show_time();
private:
	int hour;
	int minute;
	int second;
};
void Time::set_time()
{
	cin>>hour;
	cin>>minute;
	cin>>second;
}
void Time::show_time()
{
	cout<<hour<<":"<<minute<<":"<<second<<endl;
}

int main()
{
	Time t1;
	t1.set_time();
	t1.show_time();
	Time t2;
	t2.show_time();
	return 0;
}

【代码2】

#include<iostream>
using namespace std;

class Time
{
public:
	Time();//在类内对构造函数进行声明
	void set_time();
	void show_time();
private:
	int hour;
	int minute;
	int second;
};
Time::Time()//在类外定义构造函数
{
	hour=0;
	minute=0;
	second=0;
}
void Time::set_time()
{
	cin>>hour;
	cin>>minute;
	cin>>second;
}
void Time::show_time()
{
	cout<<hour<<":"<<minute<<":"<<second<<endl;
}

int main()
{
	Time t1;
	t1.set_time();
	t1.show_time();
	Time t2;
	t2.show_time();
	return 0;
}


====================================================================================================================================

例5、有两个长方柱,其长、宽、高分别为:(1)12,20,25;(2)10,30,20。分别求他们的体积。

【代码1】 编一个基于对象的程序,在类中用带参数的构造函数。

#include<iostream>
using namespace std;

class Box
{
public:
	Box(int,int,int);
	int volume();
private:
	int height;
	int width;
	int length;
};
Box::Box(int h,int w,int len)
{
	height=h;
	width=w;
	length=len;
}
int Box::volume()
{
	return (height*width*length);
}

int main()
{
	Box box1(12,25,30);
	cout<<"The volume of box1 is"<<box1.volume()<<endl;
	Box box2(15,30,21);
	cout<<"The volume of box2 is"<<box2.volume()<<endl;
	return 0;
}

【代码2】用参数初始化表对数据成员初始化

#include<iostream>
using namespace std;

class Box
{
public:
	Box(int,int,int);
	int volume();
private:
	int height;
	int width;
	int length;
};
Box::Box(int h,int w,int len):height(h),width(w),length(len){}

int Box::volume()
{
	return (height*width*length);
}

int main()
{
	Box box1(12,25,30);
	cout<<"The volume of box1 is"<<box1.volume()<<endl;
	Box box2(15,30,21);
	cout<<"The volume of box2 is"<<box2.volume()<<endl;
	return 0;
}


【代码3】编一个基于对象的程序,且定义两个构造函数,其中一个有参数,一个无参数。

#include<iostream>
using namespace std;

class Box
{
public:
	Box();
	Box(int h,int w,int len):height(h),width(w),length(len){}
	int volume();
private:
	int height;
	int width;
	int length;
};
Box::Box()  
{  
    height=10;  
    width=10;  
    length=10;  
}
int Box::volume()
{
	return (height*width*length);
}

int main()
{
	Box box1;
	cout<<"The volume of box1 is"<<box1.volume()<<endl;
	Box box2(15,30,21);
	cout<<"The volume of box2 is"<<box2.volume()<<endl;
	return 0;
}


====================================================================================================================================
例6、包含构造函数和析构函数的C++程序。(谭浩强C++:例9.5)

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

class Student
{
public:
	Student(int n,string na,char s)//教材上有误,student改为Student
	{
		num=n;
		name=na;
		sex=s;
		cout<<"Constructor called."<<num<<endl;
	}
	~Student()
	{
		cout<<"Destructor called."<<num<<endl;
	}
	void display();
private:
	int num;
	string name;//教材上有误
	char sex;
};
void Student::display()
{
	cout<<"num:"<<num<<endl;
	cout<<"name:"<<name<<endl;
	cout<<"sex:"<<sex<<endl;
}

int main()
{
	Student stu1(1,"chen",'m');
	stu1.display();

	Student stu2(2,"wang",'f');
	stu2.display();

	return 0;
}




=============================================================================================================================

例7、对象数组的使用方法(谭浩强C++:例9.6)

#include<iostream>
using namespace std;

class Box
{
public:
	Box(int h=10,int w=12,int len=15):height(h),width(w),length(len){}
	int volume();
private:
	int height;
	int width;
	int length;
};
int Box::volume()
{
	return (height*width*length);
}
int main()
{
	Box a[3]={Box(10,12,15),Box(15,18,20),Box(16,20,26)};
	cout<<"The volume of a[0] is "<<a[0].volume()<<endl;
	cout<<"The volume of a[1] is "<<a[1].volume()<<endl;
	cout<<"The volume of a[2] is "<<a[2].volume()<<endl;
	return 0;
}

====================================================================================================================================

例8、有关对象指针的使用方法(谭浩强C++:例9.7)

#include<iostream>
using namespace std;

class Time
{
public:
	Time(int h,int m,int s):hour(h),minute(m),second(s){}
	void show_time();
	int hour;
	int minute;
	int second;
};
void Time::show_time()
{
	cout<<hour<<":"<<minute<<":"<<second<<endl;
}

int main()
{
    Time t1(10,13,56);
	int *p1=&t1.hour;
	cout<<*p1<<endl;
	t1.show_time();
	Time *p2=&t1;
	p2->show_time();
	void (Time::*p3)();
	p3=&Time::show_time;
	(t1.*p3)();

	return 0;
}


====================================================================================================================================

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值