C++第三章習題


3.1 類聲明的一般格式是什么?

class 類名
{
    [private:]
        私有數據成員和成員函數
    public:
        公有數據成員和成員函數
}

 

3.2 構造函數和析構函數的主要作用是什么?它們各自有什么特性?

         構造函數是一種特殊的成員函數,它主要用於為對象分配空間,進行初始化。

構造函數的名字必須與類名相同,而不能由用戶任意命名。它可以有任意類型的參數,但不能具有返回值類型。

析構函數通常用於執行一些清理任務,如釋放分配給對象的內存空間等。

析構函數名與類名相同,但它前面必須加一個波浪號。不能有返回值,也不能有參數。

 

3.3 什么是對象數組?

         所謂對象數組,是指每一個數組元素都是對象的數組。

 

3.4 什么是this指針?它的主要作用是什么?

         C++為成員函數提供了一個名為this的指針,這個指針稱為自引用指針。每當創建一個對象時,系統就把this指針初始化為指向該對象。

         一個類的所有對象合用一份成員函數,this指針可以幫助對象辨別出當前調用的是自己的那個對象的數據成員和函數。

 

3.5 友元函數有什么作用?

         友元函數可以在類的外部訪問類的私有成員或保護成員。

 

3.6

(1)聲明並定義了P2, P3, 並用默認無參構造函數初始化。

(2)聲明並定義了P2, 並調用Point類的拷貝構造函數用P1對P2進行初始化。

(3)聲明並定義了P2, 並調用Point類的拷貝構造函數用P1對P2進行初始化。

(4)調用拷貝構造函數,將P1的成員值賦值給P4的成員。

 

3.7-3.10 BCCB

 

3.11-3.15 BAABA

 

3.16-3.17 BB

 

3.18

10,20

30,48

50,68

70,80

90,16

11,120

 

3.19

         Constructing

         10

         100

         Destructing

 

3.20

         3objects in existence

         4objects in existence after allocation

         3objects in existence after deletion

 

3.21

         Counting at0

         Counting at9

 

3.22

         Default constructor called.

         Default constructor called.

         Default constructor called.

         Construcotor:a=1,b=2

         Construcotor:a=3,b=4

         Construcotor:a=5,b=6

 

3.23

         Con.

         Copy con.

         default.

         Copy con.

 

3.24

         A=5

         B=14

         A=9

         B=14

 

3.25

         5,7

         22.25

 

3.26

         Constructing

         Constructing

         A=5

         B=15

         A=10

         B=15

         Destructing

         Destructing

 

3.27

         void pintStu();函數只有聲明,沒有定義。

         age是私有成員,不能用對象直接調用。

 

3.28

         void printStu() 和 void setSno(int s) 沒有加限定符 Student::

         void setAge(int a)在類中沒有聲明

 

3.29

         構造函數不能定義為私有。否則無法創建對象。

 

3.30 下面是一個計算器類的定義,請完成該類成員函數的實現。

class counter
{
public:
    counter(int number); 
    void increment();             //給原始值加1
    void decrement();             //給原始值減1
    int getvalue();               //取的計數器值
    int print();                  //顯示計數
private:
    int value;
};

counter::counter(int number)
{
    value = number;
}
void counter::increment()
{
    ++value;
}
void counter::decrement()
{
    --value;
}
int counter::getvalue()
{
    return value;
}
int counter::print()
{
    cout << value <<endl;
    return value;
}

 

3.31 根據注釋語句提示,實現類Date的成員函數

#include <iostream>
using namespace std;

class Date
{
public:
    void printDate();
    void setDay(int d);
    void setMonth(int m);
    void setYear(int y);
private:
    int day, month, year;
};
void Date::printDate()
{
    cout << "今天是" << year << "" << month << "" << day << "" << endl;
}
void Date::setDay(int d)
{
    day = d;
}
void Date::setMonth(int m)
{
    month = m;
}
void Date::setYear(int y)
{
    year = y;
}
int main()
{
    Date testDay;
    testDay.setDay(5);
    testDay.setMonth(10);
    testDay.setYear(2003);
    testDay.printDate();
    return 0;
}

 

3.32 建立類cylinder, cylinder的構造函數被傳遞了兩個double值,分別表示圓柱體的半徑和高度。用類cylinder計算圓柱體的體積,並存儲在一個double變量中。在類cylinder中包含一個成員函數vol,用來顯示每個cylinder對象的體積。

const int PI = 3.14;

class cylinder
{
private:
    double radius, height, volume;
public:
    cylinder(int rad, int hei);
    double getVolume();
    void vol();
};

cylinder::cylinder(int rad, int hei)
{
    radius = rad;
    height = hei;
}

double cylinder::getVolume()
{
    volume = PI * radius * radius *height;
    return volume;
}

void cylinder::vol()
{
    cout << "圓柱體的體積是: " << volume <<endl;
}

 

3.33 構建一個類book其中包含有兩個私有數據成員quprice,將qu初始化為1~5,將price初始化為qu10倍,建立一個有5個元素的數組對象。顯示每個對象數組元素的qu*price值。

class book
{
private:
    int qu, price;
public:
    book(int qu);
    int mult();
};

book::book(int q)
{
    if(q < 1 || q > 5)
    {
        qu = 1;
    }
    else
    {
        qu = q;
    }
    price = 10 * qu;
}

int book::mult()
{
    return qu * price;
}

int main()
{
    book books[5] = {1,2,3,4,5};
    for(int i = 0; i < 5; i++)
    {
        cout << books[i].mult() << " ";
    }
}

 

3.34 修改3.33,通過對象指針訪問對象數組,使程序以相反的順序顯示每個對象數組元素的qu*price值。

class book
{
private:
    int qu, price;
public:
    book(int qu);
    int mult();
};

book::book(int q)
{
    if(q < 1 || q > 5)
    {
        qu = 1;
    }
    else
    {
        qu = q;
    }
    price = 10 * qu;
}

int book::mult()
{
    return qu * price;
}

int main()
{
    book books[5] = {1,2,3,4,5};
    book *p = books;
    p += 4;
    for(int i = 0; i < 5; i++)
    {
        cout << p->mult() << " ";
        --p;
    }
    return 0;
}

 

3.35 構建一個類Stock,含字符數組stockcode[]及整型數組成員quan、雙精度型數據成員price。構造函數含3個參數:字符數組na[]qp。當定義Stock的類對象時,將對象的第一個字符串參數賦給數據成員stockcode,第2和第3個參數分別賦給quanprice。未設置第2和第3個參數時,quan的值為1000price的值為8.98.成員函數print沒有形參,需使用this指針,顯示對象數據成員的內容。編寫程序顯示對象數據成員的值。

#include <iostream>
using namespace std;

class Stock
{
private:
    char stockcode[25];
    int quan;
    double price;
public:
    Stock(char na[], int q = 1000, double p = 8.98);
    Stock(char na[]);
    void print();
};

Stock::Stock(char na[], int q = 1000, double p = 8.98)
{
    strcpy(stockcode, na);
    quan = q;
    price = p;
}

void Stock::print()
{
    cout << "stockcode: " << this->stockcode << " quan: " << this->quan << " price: " << this->price << endl;
}

int main()
{
    Stock stock1("600001", 3000, 5.67);
    Stock stock2("600002");
    stock1.print();
    stock2.print();
    return 0;
}

 

3.36 編寫一個程序,已有若干學生的數據,包括學號、姓名、成績,要求輸出這些學生的數據並計算出學生人數和平均成績(要求將學生人數和總成績用靜態數據成員表示)。

#include <iostream>
using namespace std;

class student
{
private:
    char name[25], studentNo[10];
    int score;
    static int sum;
    static int totalScore;
public:
    student(char na[], char stuNo[], int sc);
    void show();
    static void showTotal();
};

student::student(char na[], char stuNo[], int sc)
{
    strcpy(name, na);
    strcpy(studentNo, stuNo);
    score = sc;
    ++sum;
    totalScore += sc;
}

void student::show()
{
    cout << "姓名: " << name <<endl;
    cout << "學號: " << studentNo << endl;
    cout << "成績: " << score << endl;
}

void student::showTotal()
{
    cout << "總人數: " << sum << endl;
    cout << "平均成績: " << (double)totalScore/sum <<endl;
}

int student::sum = 0;
int student::totalScore = 0;

int main()
{
    student s1("張無忌", "111254", 75);
    student s2("李莫愁", "254114", 60);
    student s3("小龍女", "112587", 88);
    s1.show();
    s2.show();
    s3.show();
    student::showTotal();
return 0;
}

 

 

 


免責聲明!

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



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