C++第三章

4.
#include <iostream> 
using namespace std; 
class Student  
{
public:     
	Student(int n,float s):num(n),score(s){}    
	void display();   
private:   
	 int num;    
	 float score;  
};   
	void Student::display()   
	{cout<<num<<" "<<score<<endl;}   
int main()  
{
	Student stud[5]={    
	Student(101,78.5),Student(102,85.5),Student(103,98.5),   
	Student(104,100.0),Student(105,95.5)};  
	Student *p=stud;   
	for(int i=0;i<=2;p=p+2,i++)  
	 p->display();  
	 return 0;
} 
5.
#include <iostream> 
using namespace std; 
class Student  {
public:     
	Student(int n,float s):num(n),score(s){}    
	int num;   
 	float score;}; 
int main()  
{
	Student stud[5]={    
	Student(101,78.5),Student(102,85.5),Student(103,98.5),Student(104,100.0),Student(105,95.5)};  
	int  max(Student* ); 
	 Student *p=&stud[0];  max(p);  }   
	 int max(Student *arr) 
	  {
	  float max_score=arr[0].score; 
	   int k=0;   
	   for(int i=1;i<5;i++)     
	   if(arr[i].score>max_score) 
	   {max_score=arr[i].score;k=i;} 
	    cout<<arr[k].num<<" "<<max_score<<endl;  
	    return 0;
} 

6-12
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: 解法一
#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;}
     //可改为:void display() const {cout<<num<<" "<<score<<endl;}
  private:
   int num;
   float score;
 };

int main()
{const 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) const  {num=n;score=s;}
   void display() const {cout<<num<<" "<<score<<endl;}
  private:
   mutable int num;
   mutable float score;
 };

int main()
{const 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() {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;
}

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=0.98*rate;
  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;
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 month;
   int day;
   int year;
 };

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

void display(const Date &d,const Time &t)
 {
  cout<<d.month<<"/"<<d.day<<"/"<<d.year<<endl;
  cout<<t.hour<<":"<<t.minute<<":"<<t.sec<<endl;
 }


int main()
{
 Time t1(10,13,56);
 Date d1(12,25,2004);
 display(d1,t1);
 return 0;
}

 11:
#include <iostream>
using namespace std;
class Time;
class Date
 {public:
   Date(int,int,int);
   friend Time;
private:
   int month;
   int day;
   int year;
 };

Date::Date(int m,int d,int y):month(m),day(d),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.month<<"/"<<d.day<<"/"<<d.year<<endl;
  cout<<hour<<":"<<minute<<":"<<sec<<endl;
 }


int main()
{
 Time t1(10,13,56);
 Date d1(12,25,2004);
 t1.display(d1);
 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<<endl;
 Compare<float> cmp2(45.78,93.6);
 cout<<cmp2.max()<<" is the Maximum of two float numbers."<<endl;
cout<<cmp2.min()<<" is the Minimum of two float numbers."<<endl<<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;
 return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值