谭浩强版C++课本实例 第三章 关于类和对象的进一步讨论(14)

来源:互联网 发布:万圣节恶作剧软件 编辑:程序博客网 时间:2024/06/06 02:03

//3.1 在例2.3的基础上定义构造成员函数

#include <iostream>

using namespace std;

class Time

{

private:

int hour;

int minute;

int sec;

public:

Time()

{

hour=0;

minute=0;

sec=0;

}

void set_time();

void show_time();

};

void Time::set_time()

{

cin>>hour;

cin>>minute;

cin>>sec;

}

void Time::show_time()

{

cout<<hour<<":"<<minute<<":"<<sec<<endl;

}

int main()

{

Time t1;

t1.set_time();

t1.show_time();

Time t2;

t2.show_time();

return 0;

}

 

//3.2有两个长方柱,其长宽高分别为(1122530;(2153021。分别求它们的体积。

//编写一个程序,在类中用带参数的构造函数

#include <iostream>

using namespace std;

class Box

{

private:

int height;

int width;

int length;

public:

Box(int,int,int);

int volume();

};

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 box is "<<box1.volume()<<endl;

Box box2(15,30,21);

cout<<"The volume of box is "<<box2.volume()<<endl;

return 0;

}

 

//3.3在例3.2的基础上,定义两个构造函数,其中一个无参数,另一个有参数

#include <iostream>

using namespace std;

class Box

{

private:

int height;

int width;

int length;

public:

Box();

Box(int h,int w,int len):height(h),

width(w),length(len){}

int volume();

};

Box::Box()

{

height=10;

width=10;

length=10;

}

int Box::volume()

{

return (height*width*length);

}

int main()

{

Box box1(12,25,30);

cout<<"The volume of box is "<<box1.volume()<<endl;

Box box2(15,30,21);

cout<<"The volume of box is "<<box2.volume()<<endl;

return 0;

}

 

//3.4在例3.3程序中的构造函数改用默认的参数,长宽高的默认值均为10

#include <iostream>

using namespace std;

class Box

{

private:

int height;

int width;

int length;

public:

Box(int h=10,int w=10,int len=10);

int volume();

};

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;

cout<<"The volume of box1 is "<<box1.volume()<<endl;

Box box2(15);

cout<<"The volume of box2 is "<<box2.volume()<<endl;

Box box3(15,30);

cout<<"The volume of box3 is "<<box3.volume()<<endl;

Box box4(15,30,20);

cout<<"The volume of box4 is "<<box4.volume()<<endl;

return 0;

}

 

//3.5包含构造函数和析构函数的c++程序

#include <iostream>

#include <string>

using namespace std;

class Student

{

private:

int num;

string name;

char sex;

public:

Student(int n,string nam,char s)

num=n;

name=nam;

sex=s;

cout<<"Constructor called."<<endl;

}

~Student()

{

cout<<"Destructor called."<<endl;

}

void display()

{

cout<<"num:"<<num<<endl;

cout<<"name:"<<name<<endl;

cout<<"sex;"<<sex<<endl<<endl;

}

};

int main()

{

Student stud1(10010,"wang_li",'f');

stud1.display();

Student stud2(10011,"zhang_fun",'m');

stud2.display();

return 0;

}

 

//3.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<<"volume of a[0] is "<<a[0].volume()<<endl;

cout<<"volume of a[1] is "<<a[1].volume()<<endl;

cout<<"volume of a[2] is "<<a[2].volume()<<endl;

return 0;

}

//3.6.1 课本82也上机试试的例子 对象数组的使用方法

#include <iostream>

using namespace std;

class Box

{

private:

int height;

int width;

int length;

public:

Box(int h=10,int w=2,int len=1):height(h),width(w),length(len){}

int volume();

};

int Box::volume()

{

return (height*width*length);

}

int main()

{

Box a[3]={1,4,2};

cout<<"volume of a[0] is "<<a[0].volume()<<endl;

cout<<"volume of a[1] is "<<a[1].volume()<<endl;

cout<<"volume of a[2] is "<<a[2].volume()<<endl;

return 0;

}

//3.7有关对象指针的使用方法

#include <iostream>

using namespace std;

class Time

{

public:

int hour;

int minute;

int sec;

Time(int ,int ,int);

void get_time();

};

Time::Time(int h,int m,int s)

{

hour=h;

minute=m;

sec=s;

}

void Time::get_time()

{

cout<<hour<<":"<<minute<<":"<<sec<<endl;

}

int main()

{

Time t1(10,13,56);

int *p1=&t1.hour;

cout<<*p1<<endl;

t1.get_time();

 

Time *p2=&t1;

p2->get_time();

 

void(Time::*p3)();

p3=&Time::get_time;

(t1.*p3)();

 

Time *p4=new Time(11,11,11);

p4->get_time();

return 0;

}

 

//3.8对象的常引用

#include <iostream>

using namespace std;

class Time

{

public:

Time(int ,int ,int);

int hour;

int minute;

int sec;

};

Time::Time(int h,int m,int s)

{

hour=h;

minute=m;

sec=s;

}

void fun(Time &t)

{

t.hour=18;

}

int main()

{

Time t1(10,13,56);

    fun(t1);

cout<<t1.hour<<endl;

return 0;

}

 

//3.9对象的赋值

#include <iostream>

using namespace std;

class Box

{

private:

int height;

int width;

int length;

public:

Box(int =10,int=10,int=10);

int volume();

};

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(15,30,25),box2; //box2已经声明

cout<<"The volume of box1 is "<<box1.volume()<<endl;

box2=box1;//赋值 box2已经存在 注意与复制的区别

cout<<"The volume of box2 is "<<box2.volume()<<endl;

return 0;

}

 

//3.10引用静态数据成员

#include <iostream>

using namespace std;

class Box

{

public:

Box(int ,int);

int volume();

static int height;  //静态变量

int width;

int length;

};

Box::Box(int w,int len)

{

width=w;

length=len;

}

int Box::volume()

{

return (height*width*length);

}

int Box::height=10;//静态变量只能单独初始化

int main()

{

Box a(15,20),b(20,30);

cout<<a.height<<endl;

cout<<b.height<<endl;

cout<<Box::height<<endl;

cout<<a.volume()<<endl;

return 0;

}

 

//3.11静态成员函数的应用

#include <iostream>

using namespace std;

class Student

{

private:

int num;

int age;

float score;

static float sum;

static int count;

public:

Student(int n,int a,float s):num(n),age(a),score(s){}

void total();

static float average();

};

void Student::total()

{

sum+=score;

count++;

}

float Student::average()   //静态函数只能引用静态变量,不能引用非静态变量

{

return (sum/count);

}

float Student::sum=0;

int Student::count=0;

int main()

{

Student stud[3]={

Student(1001,18,70),

Student(1002,19,78),

Student(1005,20,98)};

int n;

cout<<"please input the number of students:";

cin>>n;

for(int i=0;i<n;i++)

stud[i].total();

cout<<"the average score of "<<n<<" students is "<<Student::average()<<endl;

return 0;

}

 

//3.11.1静态成员函数的应用 改变average的静态属性

#include <iostream>

using namespace std;

class Student

{

private:

int num;

int age;

float score;

static float sum;

static int count;

public:

Student(int n,int a,float s):num(n),age(a),score(s){}

void total();

float average();

};

void Student::total()

{

sum+=score;

count++;

}

float Student::average()

{

return (sum/count);

}

float Student::sum=0;

int Student::count=0;

int main()

{

Student stud[3]={

Student(1001,18,70),

Student(1002,19,78),

Student(1005,20,98)};

int n;

cout<<"please input the number of students:";

cin>>n;

for(int i=0;i<n;i++)

stud[i].total();

cout<<"the average score of "<<n<<" students is "<<stud[n].average()<<endl;

return 0;

}

 

//3.12友元函数的简单例子

#include <iostream>

using namespace std;

class Time

{

private:

int hour;

int minute;

int sec;

public:

Time(int ,int ,int);

friend void display(Time &);

};

Time::Time(int h,int m,int s )

{

hour=h;

minute=m;

sec=s;

}

void display(Time& t)

{

cout<<t.hour<<":"<<t.minute<<":"<<t.sec<<endl;

}

int main()

{

Time t1(10,13,56);

display(t1);

return 0;

}

 

//3.13友元成员函数的简单应用

#include <iostream>

using namespace std;

class Date;

class Time

{

private:

int hour;

int minute;

int sec;

public:

Time(int,int,int);

void display(Date &);

};

class Date

{

public:

Date(int ,int ,int);

friend void Time::display(Date&);   //友元函数

private:

int month;

int day;

int year;

};

Time::Time(int h,int m,int s)

{

hour=h;

minute=m;

sec=s;

}

void Time::display(Date &d)

{

cout<<d.month<<"/"<<d.day<<"/"<<d.year<<endl;

cout<<hour<<":"<<minute<<":"<<sec<<endl;

}

Date::Date(int m,int d,int y)

{

month=m;

day=d;

year=y;

}

int main()

{

Time t1(10,12,56);

Date d1(12,25,2004);

t1.display(d1);

return 0;

}

 

//3.13.1普通函数成为多个类的友元函数的的简单应用

#include <iostream>

using namespace std;

class Date;

class Time

{

public:

Time(int,int,int);

friend void display(Date&,Time&);

private:

int hour;

int minute;

int sec;

};

class Date

{

public:

Date(int ,int ,int);

friend void display(Date&,Time&);

private:

int month;

int day;

int year;

};

Time::Time(int h,int m,int s)

{

hour=h;

minute=m;

sec=s;

}

void display(Date &d,Time &t)  //非成员函数

{

cout<<d.month<<"/"<<d.day<<"/"<<d.year<<endl;

cout<<t.hour<<":"<<t.minute<<":"<<t.sec<<endl;

}

Date::Date(int m,int d,int y)

{

month=m;

day=d;

year=y;

}

int main()

{

Time t1(10,12,56);

Date d1(12,25,2004);

display(d1,t1);

return 0;

}

 

//3.14声明一个类模板,利用它分别实现两个整数、浮点数和字符的比较,求出大数和小数

#include <isotream>

using namespace std;

template <class numtype>

class Compare

{

private:

numtype x,y;

public:

Compare(numtype a,numtype b)

{

x=a;

y=b;

}

numtype max()

{

return (x>y)?x:y;

}

numtype min();

// numtype min()

// {

// return (x<y)?x:y;

// }

};

template <class numtype>

numtype Compare<numtype>::min()

{

return (x<y)?x:y;

}

int main()

{

Compare<int>cmpl(3,7);

cout<<cmp1.max()<<"is the Maximum of two integer numbers."<<endl;

cout<<cmp1.min()<<"is the Minimum of two integer numbers."<<endl<<endl;

    Compare<float>cmp2(45.78,69.3);

cout<<cmp2.max()<<"is the Maximum of two integer numbers."<<endl;

cout<<cmp2.min()<<"is the Minimum of two integer numbers."<<endl<<endl;

Compare<char>cmp3('a','A');

cout<<cmp3.max()<<"is the Maximum of two integer numbers."<<endl;

cout<<cmp3.min()<<"is the Minimum of two integer numbers."<<endl;

return 0;

}

0 0
原创粉丝点击