您的位置:首页 > 编程语言 > C语言/C++

c++第三章作业

2015-04-20 15:23 302 查看
C ++ 第三章作业

1、答案:

构造函数的作用:用于新建对象的初始化工作。

析构函数的作用:用于在撤销对象前,完成一些清理工作,比如:释放内存等。

每当创建对象时,需要添加初始化代码时,则需要定义自己的构造函数;

而对象撤销时,需要自己添加清理工作的代码时,则需要定义自己的析构函数。

2.略。

3.#include <iostream>//第三小题

using namespace std;

class Date

{

public:

Date(int=1,int=1,int=2005);

void display();

private:

int month;

int day;

int year;

};

Date::Date(int m,int d,int y):month(m),day(d),year(y){}

void Date::display()

{

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

}

int main()

{

Date d1(10,13,2005);

Date d2(12,30);

Date d3(10);

Date d4;

d1.display();

d2.display();

d3.display();

d4.display();

return 0;

}





4.#include <iostream>//第四小题

using namespace std;

class Student

{

public:

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

void display();

private:

int num;

int score;

};

void Student::display()

{

cout<<num<<" "<<score<<endl;

}

int main()

{

Student stud[5]={Student(1,88),Student(2,86),Student(3,90),Student(4,78),Student(5,88)};

Student *p=stud;

for(int i=0;i<3;p+=2,i++)

p->display();

return 0;

}







5.#include<iostream>//第三章第五小题

using namespace std;

class Student {

public:

Student(int n,int s):num(n),score(s){};

int num;

int score;

};

void stu_max(Student *p){

int i=0,k=0,max=p[0].score;

for(i;i<5;i++){

if(p[i].score>max)

{

max=p[i].score
;k++;}

}

cout<<"最高分"<<p[k].score<<"学号"<<p[k].num;

}

int main(){

Student std[5]={Student(1,88),Student

(2,86),Student

(3,90),Student(4,78),Student(5,88)};

Student *p=std;

stu_max(p);

return 0;

}



6.#include <iostream>//第六小题

using namespace std;

class Student

{

public:

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

void change(int n,float s)

{

num=n;

score=s;

}

void display()

{

cout<<num<<" "<<score<<endl;

}

private:

int num;

float score;

};

int main()

{

Student stud(101,78.5);

stud.display();

stud.change(101,80.5);

stud.display();

return 0;







7.(1) #include <iostream>//7.(1)小题

using namespace std;

class Student

{

public:

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

void change(int n,float s)

{

num=n;

score=s;

}

//void display()

void display()const

{

cout<<num<<" "<<score<<endl;

}

private:

int num;

float score;

};

int main()

{

const Student stud(101,78.5);

stud.display();

return 0;

}



7.(2) #include <iostream>//7.(2)小题

using namespace std;

class Student

{

public:

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

//void change(int n,float s)

void change(int n,float s)const

{

num=n;

score=s;

}

//void display()

void display()const

{

cout<<num<<" "<<score<<endl;

}

private:

//int num;

mutable int num;//将变量变为可改变的值

//float score;

mutable float score;

};

int main()

{

//Student stud(101,78.5);

const Student stud(101,78.5);

stud.display();

stud.change(101,80.5);

stud.display();

return 0;

}







7.(3)#include
<iostream>//7.(3)小题

using namespace std;

class Student

{

public:

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

void change(int n,float s)

{

num=n;

score=s;

}

void display()

{

cout<<num<<" "<<score<<endl;

}

private:

int num;

float score;

};

int main()

{

Student stud(101,78.5);

Student *p=&stud;

p->display();

p->change(101,80.5);

p->display();

return 0;

}





7.(4) #include
<iostream>//7.(4)小题

using namespace std;

class Student

{

public:

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

//void change(int n,float s)

void change(int n,float s)const

{

num=n;

score=s;

}

//void display()

void display()const

{

cout<<num<<" "<<score<<endl;

}

private:

//int num;

mutable int num;

//float score;

mutable float score;

};

int main()

{

//Student stud(101,78.5);

const Student stud(101,78.5);

stud.display();

const Student *p = &stud;

//stud.change(101,80.5);

p->change(101,80.5);

//stud.display();

p->display();

return 0;

}



7.(5)#include
<iostream>//7.(5)小题

using namespace std;

class Student

{

public:

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

//void change(int n,float s)

void change(int n,float s)const

{

num=n;

score=s;

}

//void display()

void display()const

{

cout<<num<<" "<<score<<endl;

}

private:

//int num;

mutable int num;

//float score;

mutable float score;

};

int main()

{

//Student stud(101,78.5);

const Student stud(101,78.5);

stud.display();

const Student *p = &stud;

//stud.change(101,80.5);

p->change(101,80.5);

//stud.display();

p->display();

return 0;

}




8、#include <iostream>//第八小题

using namespace std;

class Student

{

public:

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

void change(int n,float s)

{

num=n;

score=s;

}

void display()

{

cout<<num<<" "<<score<<endl;

}

private:

int num;

float score;

};

int main()

{

Student stud(101,78.5);

void fun(Student &);

fun(stud);

return 0;

}

void fun(Student &stu)

{

stu.display();

stu.change(101,80.5);

stu.display();

}








9、#include <iostream>//第九小题

using namespace std;

class Product

{

public:

Product(int n,int q,float p):num(n),quantity(q),price(p){}

void total();

static float average();

static void display();

private:

int num;

int quantity;

float price;

static float discount;

static float sum;

static int n;

};

void Product::total()

{

float rate=1.0;

if(quantity>10)

rate=rate*0.98;

sum=sum+quantity*price*rate*(1-discount);

n=n+quantity;

}

void Product::display()

{

cout<<sum<<endl;

cout<<average()<<endl;

}

float Product::average()

{

return (sum/n);

}

float Product::discount=0.05;

float Product::sum=0.0;

int Product::n=0;

int main()

{

Product prod[3]={Product(101,5,23.5),Product

(102,12,24.56),Product(103,100,21.5)};

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

prod[i].total();

Product::display();

return 0;

}







10、#include <iostream>//第十小题

using namespace std;

class Date;

class Time

{

public:

Time(int ,int ,int );

friend void display(const Date &,const Time &);

private:

int hour;

int minute;

int sec;

};

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

{

hour=h;

minute=m;

sec=s;

}

class Date

{

public:

Date(int ,int ,int );

friend void display(const Date &,const Time &);

private:

int day;

int month;

int year;

};

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

{

day=d;

month=m;

year=y;

}

void display(const Date &d,const Time &t)

{

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

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

}

int main()

{

Time t(10,13,56);

Date d(10,25,2004);

display(d,t);

return 0;

}





11、#include <iostream>//第十 一小题

using namespace std;

class Time;

class Date

{

public:

Date(int ,int ,int);

friend Time;

private:

int day;

int month;

int year;

};

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

{

int day=d;

int month=m;

int year=y;

}

class Time

{

public:

Time(int,int,int);

void display(const Date &);

private:

int hour;

int minute;

int sec;

};

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

{

hour=h;

minute=m;

sec=s;

}

void Time::display(const Date &d)

{

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

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

}

int main()

{

Time t(10,13,56);

Date d(12,25,2004);

t.display(d);

return 0;

}








12、

#include <iostream>//第十二小题

using namespace std;

template<class numtype>

class Compare

{

public:

Compare(numtype a,numtype b);

numtype max();

numtype min();

private:

numtype x,y;

};

template<class numtype>

Compare<numtype>::Compare(numtype a,numtype b)

{

x=a;

y=b;

}

template<class numtype>

numtype Compare<numtype>::max()

{

return (x>y)?x:y;

}

template<class numtype>

numtype Compare<numtype>::min()

{

return (x<y)?x:y;

}

int main()

{

Compare<int> cmp1(3,7);

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

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

Compare<float> cmp2(45.78,93.6);

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

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

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

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

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

}



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: