日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

c++基础学习(07)--(类)

發(fā)布時間:2023/12/13 c/c++ 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c++基础学习(07)--(类) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

文章目錄

  • 目錄
    • 類與對象
      • 1.類成員函數(shù)
      • 2.類訪問修飾符
      • 3.構造函數(shù)與析構函數(shù)
      • 4.拷貝構造函數(shù)
      • 5. 友元函數(shù)
      • 6.內(nèi)聯(lián)函數(shù)
      • 7.this指針
      • 8.指向類的指針
      • 9.類的靜態(tài)成員

目錄

類與對象


#include <iostream>using namespace std;class Box {public:double length; // 長度double breadth; // 寬度double height; // 高度 };int main( ) {Box Box1; // 聲明 Box1,類型為 BoxBox Box2; // 聲明 Box2,類型為 Boxdouble volume = 0.0; // 用于存儲體積// box 1 詳述Box1.height = 5.0; Box1.length = 6.0; Box1.breadth = 7.0;// box 2 詳述Box2.height = 10.0;Box2.length = 12.0;Box2.breadth = 13.0;// box 1 的體積volume = Box1.height * Box1.length * Box1.breadth;cout << "Box1 的體積:" << volume <<endl;// box 2 的體積volume = Box2.height * Box2.length * Box2.breadth;cout << "Box2 的體積:" << volume <<endl;return 0; }

當上面的代碼被編譯和執(zhí)行時,它會產(chǎn)生下列結果:
Box1 的體積:210
Box2 的體積:1560

需要注意的是,私有的成員和受保護的成員不能使用直接成員訪問運算符 (.) 來直接訪問.

1.類成員函數(shù)


#include <iostream> #include <limits>using namespace std;class Box{ public://成員變量的定義 double length;double breadth;double height;//成員函數(shù)的定義double getVolume(void);void setLength(double len);void setBreadth(double bre);void setHeight(double hei); };double Box::getVolume(void){return length*breadth*height; } void Box::setLength(double len){length = len; } void Box::setBreadth(double bre){breadth = bre; } void Box::setHeight(double hei){height = hei; }int main(){Box Box1; // 聲明 Box1,類型為 BoxBox Box2; // 聲明 Box2,類型為 Boxdouble volume = 0.0; // 用于存儲體積// box 1 詳述Box1.setLength(6.0); Box1.setBreadth(7.0); Box1.setHeight(5.0);// box 2 詳述Box2.setLength(12.0); Box2.setBreadth(13.0); Box2.setHeight(10.0);// box 1 的體積volume = Box1.getVolume();cout << "Box1 的體積:" << volume <<endl;// box 2 的體積volume = Box2.getVolume();cout << "Box2 的體積:" << volume <<endl;return 0;}

當上面的代碼被編譯和執(zhí)行時,它會產(chǎn)生下列結果:
Box1 的體積: 210
Box2 的體積: 1560

2.類訪問修飾符


公有成員Public
公有成員在程序中類的外部是可訪問的。您可以不使用任何成員函數(shù)來設置和獲取公有變量的值,如下所示:

#include <iostream>using namespace std;class Line {public:double length;void setLength( double len );double getLength( void ); };// 成員函數(shù)定義 double Line::getLength(void) {return length ; }void Line::setLength( double len ) {length = len; }// 程序的主函數(shù) int main( ) {Line line;// 設置長度line.setLength(6.0); cout << "Length of line : " << line.getLength() <<endl;// 不使用成員函數(shù)設置長度line.length = 10.0; // OK: 因為 length 是公有的cout << "Length of line : " << line.length <<endl;return 0; }

當上面的代碼被編譯和執(zhí)行時,它會產(chǎn)生下列結果:
Length of line : 6
Length of line : 10

私有成員Private
私有成員變量或函數(shù)在類的外部是不可訪問的,甚至是不可查看的。只有類和友元函數(shù)可以訪問私有成員。
默認情況下,類的所有成員都是私有的。例如在下面的類中,width 是一個私有成員,這意味著,如果您沒有使用任何訪問修飾符,類的成員將被假定為私有成員:

class Box {double width;public:double length;void setWidth( double wid );double getWidth( void ); };

實際操作中,我們一般會在私有區(qū)域定義數(shù)據(jù),在公有區(qū)域定義相關的函數(shù),以便在類的外部也可以調(diào)用這些函數(shù),如下所示:

#include <iostream>using namespace std;class Box {public:double length;void setWidth( double wid );double getWidth( void );private:double width; };// 成員函數(shù)定義 double Box::getWidth(void) {return width ; }void Box::setWidth( double wid ) {width = wid; }// 程序的主函數(shù) int main( ) {Box box;// 不使用成員函數(shù)設置長度box.length = 10.0; // OK: 因為 length 是公有的cout << "Length of box : " << box.length <<endl;// 不使用成員函數(shù)設置寬度// box.width = 10.0; // Error: 因為 width 是私有的box.setWidth(10.0); // 使用成員函數(shù)設置寬度cout << "Width of box : " << box.getWidth() <<endl;return 0; }

當上面的代碼被編譯和執(zhí)行時,它會產(chǎn)生下列結果:
Length of box : 10
Width of box : 10

保護成員Protected
保護成員變量或函數(shù)與私有成員十分相似,但有一點不同,保護成員在派生類(即子類)中是可訪問的。
在下一個章節(jié)中,您將學習到派生類和繼承的知識。現(xiàn)在您可以看到下面的實例中,我們從父類 Box 派生了一個子類 smallBox。
下面的實例與前面的實例類似,在這里 width 成員可被派生類 smallBox 的任何成員函數(shù)訪問。

#include <iostream> using namespace std;class Box {protected:double width; };class SmallBox:Box // SmallBox 是派生類 {public:void setSmallWidth( double wid );double getSmallWidth( void ); };// 子類的成員函數(shù) double SmallBox::getSmallWidth(void) {return width ; }void SmallBox::setSmallWidth( double wid ) {width = wid; }// 程序的主函數(shù) int main( ) {SmallBox box;// 使用成員函數(shù)設置寬度box.setSmallWidth(5.0);cout << "Width of box : "<< box.getSmallWidth() << endl;return 0; }

#include<iostream> #include<assert.h> using namespace std;class A{ public:int a;A(){a1 = 1;a2 = 2;a3 = 3;a = 4;}void fun(){cout << a << endl; //正確cout << a1 << endl; //正確cout << a2 << endl; //正確cout << a3 << endl; //正確} public:int a1; protected:int a2; private:int a3; }; class B : public A{ public:int a;B(int i){A();a = i;}void fun(){cout << a << endl; //正確,public成員cout << a1 << endl; //正確,基類的public成員,在派生類中仍是public成員。cout << a2 << endl; //正確,基類的protected成員,在派生類中仍是protected可以被派生類訪問。cout << a3 << endl; //錯誤,基類的private成員不能被派生類訪問。} }; int main(){B b(10);cout << b.a << endl;cout << b.a1 << endl; //正確cout << b.a2 << endl; //錯誤,類外不能訪問protected成員cout << b.a3 << endl; //錯誤,類外不能訪問private成員system("pause");return 0; }

10
1
sh: 1: pause: not found

protected 繼承

#include<iostream> #include<assert.h> using namespace std; class A{ public:int a;A(){a1 = 1;a2 = 2;a3 = 3;a = 4;}void fun(){cout << a << endl; //正確cout << a1 << endl; //正確cout << a2 << endl; //正確cout << a3 << endl; //正確} public:int a1; protected:int a2; private:int a3; }; class B : protected A{ public:int a;B(int i){A();a = i;}void fun(){cout << a << endl; //正確,public成員。cout << a1 << endl; //正確,基類的public成員,在派生類中變成了protected,可以被派生類訪問。cout << a2 << endl; //正確,基類的protected成員,在派生類中還是protected,可以被派生類訪問。cout << a3 << endl; //錯誤,基類的private成員不能被派生類訪問。} }; int main(){B b(10);cout << b.a << endl; //正確。public成員cout << b.a1 << endl; //錯誤,protected成員不能在類外訪問。cout << b.a2 << endl; //錯誤,protected成員不能在類外訪問。cout << b.a3 << endl; //錯誤,private成員不能在類外訪問。system("pause");return 0; }

private 繼承

#include<iostream> #include<assert.h> using namespace std; class A{ public:int a;A(){a1 = 1;a2 = 2;a3 = 3;a = 4;}void fun(){cout << a << endl; //正確cout << a1 << endl; //正確cout << a2 << endl; //正確cout << a3 << endl; //正確} public:int a1; protected:int a2; private:int a3; }; class B : private A{ public:int a;B(int i){A();a = i;}void fun(){cout << a << endl; //正確,public成員。cout << a1 << endl; //正確,基類public成員,在派生類中變成了private,可以被派生類訪問。cout << a2 << endl; //正確,基類的protected成員,在派生類中變成了private,可以被派生類訪問。cout << a3 << endl; //錯誤,基類的private成員不能被派生類訪問。} }; int main(){B b(10);cout << b.a << endl; //正確。public成員cout << b.a1 << endl; //錯誤,private成員不能在類外訪問。cout << b.a2 << endl; //錯誤, private成員不能在類外訪問。cout << b.a3 << endl; //錯誤,private成員不能在類外訪問。system("pause");return 0; }

3.構造函數(shù)與析構函數(shù)

#include <iostream>using namespace std;class Line {public:void setLength( double len );double getLength( void );Line(); // 這是構造函數(shù)private:double length; };// 成員函數(shù)定義,包括構造函數(shù) Line::Line(void) {cout << "Object is being created" << endl; }void Line::setLength( double len ) {length = len; }double Line::getLength( void ) {return length; } // 程序的主函數(shù) int main( ) {Line line;// 設置長度line.setLength(6.0); cout << "Length of line : " << line.getLength() <<endl;return 0; }

當上面的代碼被編譯和執(zhí)行時,它會產(chǎn)生下列結果:
Object is being created
Length of line : 6

帶參數(shù)的構造函數(shù)
默認的構造函數(shù)沒有任何參數(shù),但如果需要,構造函數(shù)也可以帶有參數(shù)。這樣在創(chuàng)建對象時就會給對象賦初始值,如下面的例子所示:

#include <iostream>using namespace std;class Line {public:void setLength( double len );double getLength( void );Line(double len); // 這是構造函數(shù)private:double length; };// 成員函數(shù)定義,包括構造函數(shù) Line::Line( double len) {cout << "Object is being created, length = " << len << endl;length = len; }void Line::setLength( double len ) {length = len; }double Line::getLength( void ) {return length; } // 程序的主函數(shù) int main( ) {Line line(10.0);// 獲取默認設置的長度cout << "Length of line : " << line.getLength() <<endl;// 再次設置長度line.setLength(6.0); cout << "Length of line : " << line.getLength() <<endl;return 0; }

當上面的代碼被編譯和執(zhí)行時,它會產(chǎn)生下列結果:
Object is being created, length = 10
Length of line : 10
Length of line : 6

類的析構函數(shù)
類的析構函數(shù)是類的一種特殊的成員函數(shù),它會在每次刪除所創(chuàng)建的對象時執(zhí)行。
析構函數(shù)的名稱與類的名稱是完全相同的,只是在前面加了個波浪號(~)作為前綴,它不會返回任何值,也不能帶有任何參數(shù)。析構函數(shù)有助于在跳出程序(比如關閉文件、釋放內(nèi)存等)前釋放資源。
下面的實例有助于更好地理解析構函數(shù)的概念:

#include <iostream>using namespace std;class Line {public:void setLength( double len );double getLength( void );Line(); // 這是構造函數(shù)聲明~Line(); // 這是析構函數(shù)聲明private:double length; };// 成員函數(shù)定義,包括構造函數(shù) Line::Line(void) {cout << "Object is being created" << endl; } Line::~Line(void) {cout << "Object is being deleted" << endl; }void Line::setLength( double len ) {length = len; }double Line::getLength( void ) {return length; } // 程序的主函數(shù) int main( ) {Line line;// 設置長度line.setLength(6.0); cout << "Length of line : " << line.getLength() <<endl;return 0; }

當上面的代碼被編譯和執(zhí)行時,它會產(chǎn)生下列結果:
Object is being created
Length of line : 6
Object is being deleted

4.拷貝構造函數(shù)

#include <iostream>using namespace std;class Line {public:int getLength( void );Line( int len ); // 簡單的構造函數(shù)Line( const Line &obj); // 拷貝構造函數(shù)~Line(); // 析構函數(shù)private:int *ptr; };// 成員函數(shù)定義,包括構造函數(shù) Line::Line(int len) {cout << "調(diào)用構造函數(shù)" << endl;// 為指針分配內(nèi)存ptr = new int;*ptr = len; }Line::Line(const Line &obj) {cout << "調(diào)用拷貝構造函數(shù)并為指針 ptr 分配內(nèi)存" << endl;ptr = new int;*ptr = *obj.ptr; // 拷貝值 }Line::~Line(void) {cout << "釋放內(nèi)存" << endl;delete ptr; } int Line::getLength( void ) {return *ptr; }void display(Line obj) {cout << "line 大小 : " << obj.getLength() <<endl; }// 程序的主函數(shù) int main( ) {Line line(10);display(line);return 0; }

當上面的代碼被編譯和執(zhí)行時,它會產(chǎn)生下列結果:
調(diào)用構造函數(shù)
調(diào)用拷貝構造函數(shù)并為指針 ptr 分配內(nèi)存
line 大小 : 10
釋放內(nèi)存
釋放內(nèi)存

下面的實例對上面的實例稍作修改,通過使用已有的同類型的對象來初始化新創(chuàng)建的對象:

#include <iostream>using namespace std;class Line {public:int getLength( void );Line( int len ); // 簡單的構造函數(shù)Line( const Line &obj); // 拷貝構造函數(shù)~Line(); // 析構函數(shù)private:int *ptr; };// 成員函數(shù)定義,包括構造函數(shù) Line::Line(int len) {cout << "調(diào)用構造函數(shù)" << endl;// 為指針分配內(nèi)存ptr = new int;*ptr = len; }Line::Line(const Line &obj) {cout << "調(diào)用拷貝構造函數(shù)并為指針 ptr 分配內(nèi)存" << endl;ptr = new int;*ptr = *obj.ptr; // 拷貝值 }Line::~Line(void) {cout << "釋放內(nèi)存" << endl;delete ptr; } int Line::getLength( void ) {return *ptr; }void display(Line obj) {cout << "line 大小 : " << obj.getLength() <<endl; }// 程序的主函數(shù) int main( ) {Line line1(10);Line line2 = line1; // 這里也調(diào)用了拷貝構造函數(shù)display(line1);display(line2);return 0; }

當上面的代碼被編譯和執(zhí)行時,它會產(chǎn)生下列結果:
調(diào)用構造函數(shù)
調(diào)用拷貝構造函數(shù)并為指針 ptr 分配內(nèi)存
調(diào)用拷貝構造函數(shù)并為指針 ptr 分配內(nèi)存
line 大小 : 10
釋放內(nèi)存
調(diào)用拷貝構造函數(shù)并為指針 ptr 分配內(nèi)存
line 大小 : 10
釋放內(nèi)存
釋放內(nèi)存
釋放內(nèi)存

5. 友元函數(shù)

#include <iostream>using namespace std;class Box {double width; public:friend void printWidth( Box box );void setWidth( double wid ); };// 成員函數(shù)定義 void Box::setWidth( double wid ) {width = wid; }// 請注意:printWidth() 不是任何類的成員函數(shù) void printWidth( Box box ) {/* 因為 printWidth() 是 Box 的友元,它可以直接訪問該類的任何成員 */cout << "Width of box : " << box.width <<endl; }// 程序的主函數(shù) int main( ) {Box box;// 使用成員函數(shù)設置寬度box.setWidth(10.0);// 使用友元函數(shù)輸出寬度printWidth( box );return 0; }

當上面的代碼被編譯和執(zhí)行時,它會產(chǎn)生下列結果:
Width of box : 10

6.內(nèi)聯(lián)函數(shù)

#include <iostream>using namespace std;inline int Max(int x, int y) {return (x > y)? x : y; }// 程序的主函數(shù) int main( ) {cout << "Max (20,10): " << Max(20,10) << endl;cout << "Max (0,200): " << Max(0,200) << endl;cout << "Max (100,1010): " << Max(100,1010) << endl;return 0; }

當上面的代碼被編譯和執(zhí)行時,它會產(chǎn)生下列結果:
Max (20,10): 20
Max (0,200): 200
Max (100,1010): 1010

7.this指針

#include <iostream>using namespace std;class Box {public:// 構造函數(shù)定義Box(double l=2.0, double b=2.0, double h=2.0){cout <<"Constructor called." << endl;length = l;breadth = b;height = h;}double Volume(){return length * breadth * height;}int compare(Box box){return this->Volume() > box.Volume();}private:double length; // Length of a boxdouble breadth; // Breadth of a boxdouble height; // Height of a box };int main(void) {Box Box1(3.3, 1.2, 1.5); // Declare box1Box Box2(8.5, 6.0, 2.0); // Declare box2if(Box1.compare(Box2)){cout << "Box2 is smaller than Box1" <<endl;}else{cout << "Box2 is equal to or larger than Box1" <<endl;}return 0; }

當上面的代碼被編譯和執(zhí)行時,它會產(chǎn)生下列結果:
Constructor called.
Constructor called.
Box2 is equal to or larger than Box1

8.指向類的指針

一個指向 C++ 類的指針與指向結構的指針類似,訪問指向類的指針的成員,需要使用成員訪問運算符 ->,就像訪問指向結構的指針一樣。與所有的指針一樣,您必須在使用指針之前,對指針進行初始化。
下面的實例有助于更好地理解指向類的指針的概念:

#include <iostream>using namespace std;class Box {public:// 構造函數(shù)定義Box(double l=2.0, double b=2.0, double h=2.0){cout <<"Constructor called." << endl;length = l;breadth = b;height = h;}double Volume(){return length * breadth * height;}private:double length; // Length of a boxdouble breadth; // Breadth of a boxdouble height; // Height of a box };int main(void) {Box Box1(3.3, 1.2, 1.5); // Declare box1Box Box2(8.5, 6.0, 2.0); // Declare box2Box *ptrBox = &Box1;; // Declare pointer to a class.// 保存第一個對象的地址// 現(xiàn)在嘗試使用成員訪問運算符來訪問成員cout << "Volume of Box1: " << ptrBox->Volume() << endl;// 保存第二個對象的地址ptrBox = &Box2;// 現(xiàn)在嘗試使用成員訪問運算符來訪問成員cout << "Volume of Box2: " << ptrBox->Volume() << endl;return 0; }

當上面的代碼被編譯和執(zhí)行時,它會產(chǎn)生下列結果:
Constructor called.
Constructor called.
Volume of Box1: 5.94
Volume of Box2: 102

9.類的靜態(tài)成員

我們可以使用 static 關鍵字來把類成員定義為靜態(tài)的。當我們聲明類的成員為靜態(tài)時,這意味著無論創(chuàng)建多少個類的對象,靜態(tài)成員都只有一個副本。
靜態(tài)成員在類的所有對象中是共享的。如果不存在其他的初始化語句,在創(chuàng)建第一個對象時,所有的靜態(tài)數(shù)據(jù)都會被初始化為零。我們不能把靜態(tài)成員的初始化放置在類的定義中,但是可以在類的外部通過使用范圍解析運算符 :: 來重新聲明靜態(tài)變量從而對它進行初始化,如下面的實例所示。
下面的實例有助于更好地理解靜態(tài)成員數(shù)據(jù)的概念:

#include <iostream>using namespace std;class Box {public:static int objectCount;// 構造函數(shù)定義Box(double l=2.0, double b=2.0, double h=2.0){cout <<"Constructor called." << endl;length = l;breadth = b;height = h;// 每次創(chuàng)建對象時增加 1objectCount++;}double Volume(){return length * breadth * height;}private:double length; // 長度double breadth; // 寬度double height; // 高度 };// 初始化類 Box 的靜態(tài)成員 int Box::objectCount = 0;int main(void) {Box Box1(3.3, 1.2, 1.5); // 聲明 box1Box Box2(8.5, 6.0, 2.0); // 聲明 box2// 輸出對象的總數(shù)cout << "Total objects: " << Box::objectCount << endl;return 0; }

當上面的代碼被編譯和執(zhí)行時,它會產(chǎn)生下列結果:
Constructor called.
Constructor called.
Total objects: 2

#include <iostream>using namespace std;class Box {public:static int objectCount;// 構造函數(shù)定義Box(double l=2.0, double b=2.0, double h=2.0){cout <<"Constructor called." << endl;length = l;breadth = b;height = h;// 每次創(chuàng)建對象時增加 1objectCount++;}double Volume(){return length * breadth * height;}static int getCount(){return objectCount;}private:double length; // 長度double breadth; // 寬度double height; // 高度 };// 初始化類 Box 的靜態(tài)成員 int Box::objectCount = 0;int main(void) {// 在創(chuàng)建對象之前輸出對象的總數(shù)cout << "Inital Stage Count: " << Box::getCount() << endl;Box Box1(3.3, 1.2, 1.5); // 聲明 box1Box Box2(8.5, 6.0, 2.0); // 聲明 box2// 在創(chuàng)建對象之后輸出對象的總數(shù)cout << "Final Stage Count: " << Box::getCount() << endl;return 0; }

當上面的代碼被編譯和執(zhí)行時,它會產(chǎn)生下列結果:
Inital Stage Count: 0
Constructor called.
Constructor called.
Final Stage Count: 2

靜態(tài)成員變量在類中僅僅是聲明,沒有定義,所以要在類的外面定義,實際上是給靜態(tài)成員變量分配內(nèi)存。如果不加定義就會報錯,初始化是賦一個初始值,而定義是分配內(nèi)存。

總結

以上是生活随笔為你收集整理的c++基础学习(07)--(类)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。