例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;
}
====================================================================================================================================