第三章习题

第三题
#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;
}


第四题

#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;
}



第五题

#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;

}



第六题

#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;
}

第七题
#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()
void display()const
{
cout<<num<<" "<<score<<endl;
}
private:
int num;
float score;
};
int main()
{
 
 const Student stud(101,78.5);
 stud.display();
 
 return 0;
}

 #include <iostream>
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;
}


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


 #include <iostream>
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;
}


#include <iostream>
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;
}


 第八题

#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();
}

第九题

#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;
}


第十题

#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;
}



第十一题

#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;
}


第十二题

#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()<<"最大."<<endl;
cout<<cmp1.min()<<"最小."<<endl;
Compare<float> cmp2(45.78,93.6);
cout<<cmp2.max()<<"最大."<<endl;
cout<<cmp2.min()<<"最小."<<endl;
Compare<char> cmp3('a','A');
cout<<cmp3.max()<<"最大."<<endl;
cout<<cmp3.min()<<"最小."<<endl;


}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值