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);
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
{
int num;
int score;
public:
Student();
void display();
};
Student::Student()
{
cin>>num>>score;
}
void Student::display()
{
cout<<num<<","<<score<<endl;
}
int main()
{
Student stu[5];
for(int i=0;i<5;i++)
stu[i];
for(i=0;i<5;i=i+2)
stu[i].display();
return 0;
}
解题二:
#include<iostream>
using namespace std;
class Student
{
int num;
int score;
public:
Student();
void display();
};
Student::Student()
{
cin>>num>>score;
}
void Student::display()
{
cout<<num<<","<<score<<endl;
}
int main()
{
Student stu[5];
Student *p=NULL;
for(int i=0;i<5;i++)
stu[i];
for(i=0;i<5;i=i+2)
{
Student *p;
p=&stu[i];
p->display();
}
return 0;
}
5.
#include<iostream>
using namespace std;
class Student
{
int num;
int score;
public:
Student();
void display();
void max(Student *p);
};
Student::Student()
{
cin>>num>>score;
}
void Student::max(Student *p)
{
int max_score=p[0].score;
int k=0;
for(int i=1;i<5;i++)
if(p[i].score>max_score)
{
max_score=p[i].score;
k=i;
}
cout<<p[k].num<<","<<max_score<<endl;
}
int main()
{
Student stu[5];
Student *p=NULL;
for(int i=0;i<5;i++)
stu[i];
p=&stu[0];
p->max(p);
return 0;
}
6.
#include<iostream>
using namespace std;
class Student
{
int num;
float score;
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;};
};
int main()
{
Student stud(101,78.5);
stud.display();
stud.change(101,80.5);
stud.display();
return 0;
}
7.(2)
#include<iostream>
using namespace std;
class Student
{
int num;
float score;
public:
Student(int n,float s):num(n),score(s){}
void change(int n,float s){num=n;score=s;}
void display() const{cout<<num<<" "<<score<<endl;};
};
int main()
{
const Student stud(101,78.5);
stud.display();
//stud.change(101,80.5);
stud.display();
return 0;
}
(3)
#include<iostream>
using namespace std;
class Student
{
mutable int num;
mutable float score;
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;};
};
int main()
{
Student stud(101,78.5);
Student *p=&stud;
p->display();
p->change(101,80.5);
p->display();
return 0;
}
(5)
#include<iostream>
using namespace std;
class Student
{
mutable int num;
mutable float score;
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;};
};
int main()
{
Student stud(101,78.5);
Student * const p=&stud;
p->display();
p->change(101,80.5);
p->display();
return 0;
}
8.
#include<iostream>
using namespace std;
class Student
{
mutable int num;
mutable float score;
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 fun(Student &t)
{t.change(101,80.5);
t.display();
}
};
int main()
{
Student stud(101,78.5);
stud.display();
stud.fun(stud);
return 0;
}
9.
#include<iostream>
using namespace std;
class Dis
{
int num;
int quantity;
float price;
static float sum;
static int n;
static float discount;
public:
Dis(int m,int q,float p):num(m),quantity(q),price(p){}
static void display();
void total();
static float average();
};
void Dis::total()
{
float c=0;
n+=quantity;
if(quantity>10)
c=price*discount*quantity;
else c=price*quantity;
sum+=c;
}
void Dis::display()
{cout<<sum<<endl;
cout<<average()<<endl;}
float Dis::average()
{
float c=0;
c=sum/n;
return (c);
}
float Dis::sum=0;
int Dis::n=0;
float Dis::discount=0.98;
int main()
{
Dis d1(101,5,23.5);
Dis d2(102,12,24.56);
Dis d3(103,100,21.5);
d1.total();
d2.total();
d3.total();
Dis::display();
return 0;
}