length()函式_Miles的C++學習筆記#5.3 建構函式與解構函式在基類和派生類中的應用...
阿新 • • 發佈:2020-12-27
技術標籤:length()函式
概念:基類的建構函式在派生類的建構函式之前被呼叫,基類的解構函式在派生類的解構函式之後被呼叫
看下面這個案例你會對此有更好的認識
// This program demonstrates the order in which base and // derived class constructors and destructors are called. #include <iostream> using namespace std; //******************************** // BaseClass declaration * //******************************** class BaseClass { public: BaseClass() // Constructor { cout << "This is the BaseClass constructor." << endl; } ~BaseClass() // Destructor { cout << "This is the BaseClass destructor." << endl; } }; //******************************** // DerivedClass declaration * //******************************** class DerivedClass : public BaseClass { public: DerivedClass() // Constructor { cout << "This is the DerivedClass constructor." << endl; } ~DerivedClass() // Destructor { cout << "This is the DerivedClass destructor." << endl; } }; //******************************** // main function * //******************************** int main() { cout << "We will now define a DerivedClass object.n"; DerivedClass object; cout << "The program is now going to end.n"; return 0; }
在上面這個程式中,基類和派生類都有其預設的建構函式。但是如果我們想讓建構函式帶有引數或者是我們想創造多於1個的建構函式的話,我們就可以如同下面這樣做
#ifndef RECTANGLE_H #define RECTANGLE_H class Rectangle { private: double width; double length; public: // Default constructor Rectangle() { width = 0.0; length = 0.0; } // Constructor #2 Rectangle(double w, double len) { width = w; length = len; } double getWidth() const { return width; } double getLength() const { return length; } double getArea() const { return width * length; } }; #endif
上面這個類是設計用來儲存關於Rectangle的資料的,裡面有兩個建構函式,第一個初始化了width和length成員變數到0.0,然後第二個建構函式的引數賦值給第一個建構函式中初始化完成的兩個成員變數,我們現在來看看這個類的派生類
#ifndef BOX_H #define BOX_H #include "Rectangle.h" class Box : public Rectangle { protected: double height; double volume; public: // Default constructor Box() : Rectangle() { height = 0.0; volume = 0.0; } // Constructor #2 Box(double w, double len, double h) : Rectangle(w, len) { height = h; volume = getArea() * h; } double getHeight() const { return height; } double getVolume() const { return volume; } }; #endif
這個類是設計用來儲存關於box的資料,也就是說這個類不僅僅有length和width這兩個成員變數,還有height和volume這兩個新增的成員變數。在上面的程式中有這樣一行程式碼
請注意在建構函式的標頭中新增的符號。 在派生類建構函式的括號後面放置一個冒號,然後是對基類建構函式的函式呼叫。 在這種情況下,將呼叫基類的預設建構函式。 當這個Box類的建構函式執行時,它將首先呼叫Rectangle類的預設建構函式。 這在這裡說明
一般來說這種型別的建構函式的常用書寫格式是這樣的:
比如上面那個Box,就可以這樣寫
最後,來看看主程式
// This program demonstrates passing arguments to a base
// class constructor.
#include <iostream>
#include "Box.h"
using namespace std;
int main()
{
double boxWidth; // To hold the box's width
double boxLength; // To hold the box's length
double boxHeight; // To hold the box's height
// Get the width, length, and height from the user.
cout << "Enter the dimensions of a box:n";
cout << "Width: ";
cin >> boxWidth;
cout << "Length: ";
cin >> boxLength;
cout << "Height: ";
cin >> boxHeight;
// Define a Box object.
Box myBox(boxWidth, boxLength, boxHeight);
// Display the Box object's properties.
cout << "Here are the box's properties:n";
cout << "Width: " << myBox.getWidth() << endl;
cout << "Length: " << myBox.getLength() << endl;
cout << "Height: " << myBox.getHeight() << endl;
cout << "Base area: " << myBox.getArea() << endl;
cout << "Volume: " << myBox.getVolume() << endl;
return 0;
}