<img src="https://img-blog.csdn.net/20150420224654282" alt="" />7.(1) #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()
{
//Student stud(101,78.5);
const Student stud(101,78.5);
stud.display();
//stud.change(101,80.5);
//stud.display();
return 0;
}
<img src="https://img-blog.csdn.net/20150420224154075" alt="" />
(2) #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;
}
<img src="https://img-blog.csdn.net/20150420224115583" alt="" />
(3) #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;
}
<img src="https://img-blog.csdn.net/20150420224131152" alt="" />
(4) #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;
}
<img src="https://img-blog.csdn.net/20150420224201868" alt="" />
(5)#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;
}
<img src="https://img-blog.csdn.net/20150420224502164" alt="" />
(5)#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;
Student * const 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();
}
<img src="https://img-blog.csdn.net/20150420224533864" alt="" />
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;
}
<img src="https://img-blog.csdn.net/20150420224447213" alt="" />
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;
}
<img src="https://img-blog.csdn.net/20150420224654282" alt="" />
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;
}
<img src="https://img-blog.csdn.net/20150420225311147" alt="" />
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;
}
<img src="https://img-blog.csdn.net/20150420225331302" alt="" />