《C++ 面向对象程序设计(第2版) 谭浩强编》——课后习题之编程题——第3章
3.2 分析下面的程序...
#include <iostream>
using namespace std;
class Date
{
public:
Date(int, int, int);
Date(int, int);
Date(int);
Date();
void display();
private:
int month;
int day;
int year;
};
Date::Date(int m,int d,int y):month(m),day(d),year(y)
{ }
Date::Date(int m,int d):month(m),day(d)
{
year = 2005;
}
Date::Date(int m):month(m)
{
day = 1;
year = 2005;
}
Date::Date()
{
month = 1;
day = 1;
year = 2005;
}
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;
}
运行结果:
10/13/2005
12/30/2005
10/1/2005
1/1/2005
3.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);//month =10 day = 13 year = 2005
Date d2(12,30);//第三个个参数为默认值 month =12 day = 30 year = 2005
Date d3(10);//后两个参数为默认值 month =10 day = 1 year = 2005
Date d4; //三个参数全为默认值 month =1 day = 1 year = 2005
d1.display();
d2.display();
d3.display();
d4.display();
return 0;
}
运行结果:
同上
3.4 建立一个对象数组,内放5个学生的数据(学号,成绩),用指针指向数组首元素,输出第1,3,5学生的数据。
#include<iostream>
#include<string>
using namespace std;
class Student
{
public:
Student(string, double);//声明含参的构造函数
void display();
private:
string stuNumber;
double score;
};
Student::Student(string num, double grade):stuNumber(num), score(grade) //定义构造函数
{
}
void Student::display()
{
cout<<stuNumber<<":"<<score<<endl;
}
int main()
{
Student stus[]=
{
Student("2019001",87),
Student("2019002",90),
Student("2019003",80),
Student("2019004",97),
Student("2019005",93),
};
Student *p1 = stus;
p1->display();
(p1+2)->display();
(p1+4)->display();
return 0;
}
运行结果:
3.5 建立一个对象数组,内放5个学生的数据(学号,成绩),用指向对象的指针做函数参数,在max函数中找出5个学生中成绩最高者,并输出其学号。
#include<iostream>
#include<string>
using namespace std;
class Student
{
public:
Student(string, double);//声明含参的构造函数
friend void max(Student *);//声明友元函数
private:
string stuNumber;
double score;
};
Student::Student(string num, double grade):stuNumber(num), score(grade) //定义构造函数
{
}
void max(Student *pt)
{
double maxScore = pt->score;
int flag = 0;
for(int i = 0; i < 4; i++)
{
if(maxScore < (pt+i)->score)
{
maxScore = (pt+i)->score; //将指向较大值的指针赋给指向maxScore指针
flag = i;
}
}
cout << "The highest score is: " << maxScore << endl;
cout << "No." << (pt + flag)->stuNumber << endl;
}
int main()
{
Student stus[]=
{
Student("2019001",87),
Student("2019002",90),
Student("2019003",80),
Student("2019004",97),
Student("2019005",93),
};
Student *p1 = stus;
max(p1);
return 0;
}
运行结果:
3.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;
}
运行结果:
101 78.5
101 80.5
3.7 将第6题的程序分别作以下修改,分析所修改部分的含义以及编译和运行的情况。
(1) 将main函数第2行改为
const Student stud(101,78.5);
运行结果:编译错误
(2) 在(1)的基础上修改程序,使之能正常运行,用change函数修改数据成员num和score的值。
#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;
}
(3) 将main函数改为
int main( )
{Student stud(101,78.5);
Student *p=&stud;
p->display( );
p->change(101,80.5);
p->display( );
return 0;
其他部分仍同第6题的程序。
(4) 在(3)的基础上将main函数第3行改为const Student *p=&stud;
#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 ()const
{
cout << num << " " << score << endl;
}
private:
int num;
float score;
};
int main()
{
Student stud(101,78.5);
const Student *p=&stud;
p->display();
stud.change(101,80.5);
p->display();
return 0;
}
(5) 再把main函数第3行改为Student *const p=&stud;
#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 ()const
{
cout << num << " " << score << endl;
}
private:
int num;
float score;
};
int main()
{
Student stud(101,78.5);
Student * const p=&stud;
p->display();
stud.change(101,80.5);
p->display();
return 0;
}