27 C 基礎 類和物件的使用

2021-08-19 23:13:23 字數 1298 閱讀 6338

1.1 第一種方法:訪問物件

// 第一種方法:訪問物件

student mstudent;

mstudent.setname("李白");

result = mstudent.getname();

cout<<"name : "

<< result << endl;

1.2 第二種方法:通過指標進行方法
// 第二種方法:通過指標進行方法

student* p =

&mstudent;

result = p->getname();

cout<<

"name : "

<< result << endl;

1.3 第三種方法:通過引用進行訪問
// 第三種方法:通過引用進行訪問

student &s = mstudent;

result = s.getname();

cout

<<"name : "

<< result << endl;

return

0;

1.4 demo
#include 

#include

#include

using

namespace

std;

class student

string getname()

private:

string name;

};int main()

2.1 定義標頭檔案

主要放物件的宣告

#ifndef student_h

#define student_h

#include

#include

using namespace std;

// 放置宣告

class student ;

#endif

2.2 物件的實現
#include

"student.h"

#include

<

string

>

string student :: getname()

void student ::setname(string vaule)

2.3 訪問物件
#include 

#include "student.h"

int main()

2 7 C語言基礎

2.7 當乙個表示式中多個資料的型別不一致的時候計算機會首先把它們轉換過程由計算機自動完成,叫做隱式型別轉換 隱式型別轉換過程中會把char和short型別資料轉換成整數型別 如果既有整數型別資料又有浮點型別資料則會把整數型別轉換成浮點型別 如果所有資料的型別不同但所佔空間大小一樣,則吧整數轉換成單...

C 複習之 物件和類2(7)

下面宣告乙個類方法。const stock topval const stock s const 該類方法函式隱式地訪問乙個物件 預設可以訪問該類的私有成員 而顯式地訪問另乙個物件,並返回乙個物件的引用。括號中的const表明,該函式不會修改被顯式訪問的物件 而括號後的const表明,該函式不會修改...

c 基礎 類和物件

封裝 繼承 多型 所謂封裝就是使用三個訪問限定符來限制成員變數和成員方法的讀取許可權 1.public 修飾,任意位置可見 2.private 修飾,本類中可見 3.protected 修飾,在本類中和子類類中可見 在這裡說明,成員方法在類內實現,系統會預設其為inline函式,建議以inline函...