C++面向對象程序設計第三章習題答案解析


整理一下自己寫的作業,供考試前復習用,哈哈

進入正題!!!

題目:

2.分析下面的程序,寫出其運行時的輸出結果

這里就不展示課本源代碼,直接給出修改后的代碼,錯誤部分代碼已給出具體的注釋

 1 #include<iostream>
 2 //原題的#include<iostream.h>寫法錯誤
 3 #include<stdlib.h>
 4 //用於解決閃屏的頭文件
5 using namespace std;
 6 //原題缺少該行代碼,用於輸入cin和輸出cout
 7 class Date{
 8 public:
 9     Date(int,int,int);
10     Date(int,int);
11     Date(int);
12     Date();
13     
14     void display();
15 private:
16     int month;
17     int day;
18     int year;
19 
20 };
21 
22 Date::Date(int m,int d,int y):month(m),day(d),year(y){}
23 
24 Date::Date(int m,int d):month(m),day(d)
25 {year=2005;}
26 
27 Date::Date(int m):month(m)
28 {day=1;year=2005;}
29 
30 Date::Date()
31 {month=1;day=1;year=2005;}
32 
33 void Date::display()
34 {
35     cout<<month<<"/"<<day<<"/"<<year<<endl;
36 }
37 
38 int main()
39 {
40     Date d1(10,13,2005);
41     Date d2(12,30);
42     Date d3(10);
43     Date d4;
44     d1.display();
45     d2.display();
46     d3.display();
47     d4.display();
48     system("pause");
49     //解決閃屏的代碼
50     return 0;
51 }

運行結果:

 

3.如果將第2題中程序的第四行改為用默認參數,即

Date(int =1,int =1,int =2005);

分析程序有無問題。上機編譯,分析出錯信息,修改程序使之能通過編譯。要求保留上面一行給出的構造函數,同時能輸出與第2 題程序相同的輸出結果。

解析:在第2題的代碼上進行修改。

錯誤點:

 

 

 修改錯誤,編譯成功的代碼:

 1 #include<iostream>
 2 //原題的#include<iostream.h>寫法錯誤
 3 #include<stdlib.h>
 4 //用於解決閃屏的頭文件
 5 using namespace std;
 6 //原題缺少該行代碼,用於輸入cin和輸出cout
 7 class Date{
 8 public:
 9     Date(int x=1,int y=1,int z=2005);
10     Date(int,int);
11     Date(int);
12     Date();
13     
14     void display();
15 private:
16     int month;
17     int day;
18     int year;
19 
20 };
21 
22 Date::Date(int m,int d,int y):month(m),day(d),year(y){}
23 
24 Date::Date(int m,int d):month(m),day(d)
25 {year=2005;}
26 
27 Date::Date(int m):month(m)
28 {day=1;year=2005;}
29 
30 Date::Date()
31 {month=1;day=1;year=2005;}
32 
33 void Date::display()
34 {
35     cout<<month<<"/"<<day<<"/"<<year<<endl;
36 }
37 
38 int main()
39 {
40     Date d1(10,13,2005);
41     Date d2(10,30,2005);
42     Date d3(10,1,2005);
43     Date d4(1,1,2005);
44     d1.display();
45     d2.display();
46     d3.display();
47     d4.display();
48     system("pause");
49     //解決閃屏的代碼
50     return 0;
51 }

運行結果:

 

 

使用該錯誤修改方法,實際上就只有一個構造函數起作用,注釋其它三個構造函數,輸出結果不變,調用原理是多次調用同一個函數,而不是分別調用各自的構造函數。

源代碼:

#include<iostream>
//原題的#include<iostream.h>寫法錯誤
#include<stdlib.h>
//用於解決閃屏的頭文件
using namespace std;
//原題缺少該行代碼,用於輸入cin和輸出cout
class Date{
public:
    Date(int x=1,int y=1,int z=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(10,30,2005);
    Date d3(10,1,2005);
    Date d4(1,1,2005);
    d1.display();
    d2.display();
    d3.display();
    d4.display();
    system("pause");
    //解決閃屏的代碼
    return 0;
}

4.建立一個對象數組,內放5個學生的數據(學號,成績),用指針指向數組首元素,輸出1,3,5個學生的數據。

源代碼及解析:

 1 #include<iostream>
 2 #include<stdlib.h>
 3 using namespace std;
 4 class Student
 5 {
 6 public:
 7     //學生數據的構造函數
 8     Student(int sno,int grade):Sno(sno),Grade(grade){}
 9     void display();
10     int Sno;
11     int Grade;
12 };
13 
14 void Student::display()
15 {
16     cout<<Sno<<' '<<Grade<<endl;
17 }
18 
19 int main()
20 {
21     //五個學生的數據信息
22     Student stu[5]={
23     Student(1001,89),
24     Student(1002,90),
25     Student(1003,91),
26     Student(1004,92),
27     Student(1005,93),
28     };
29     //定義指針
30     void(Student::*p1)();
31     p1=&Student::display;
32     //調用函數
33     (stu[0].*p1)();
34     (stu[2].*p1)();
35     (stu[4].*p1)();
36     system("pause");
37     return 0;
38     
39     
40 }

運行結果:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM