C++類與對(duì)象

使用Windows窗口

#include<stdio.h>
#include<Windows.h>

int main()
{
    MessageBox(NULL, "Mike can drive!", "Driving", MB_YESNO);
    return 0;
}

程序運(yùn)行的結(jié)果如下圖所示:


001.JPG

使用面向過程設(shè)計(jì)方法計(jì)算圓的周長和面積

#include<stdio.h>

int main()
{
    double r = 0, girth, area;
    const double PI = 3.14;
    scanf("%lf", &r);
    girth = 2 * PI * r;
    area = PI * r * r;
    printf("%lf,%lf", girth, area);
    while (1);
    return 0;
}

程序的運(yùn)行結(jié)果如圖示:


002.JPG

同樣的功能采用C++語言的方式實(shí)現(xiàn)

#include<iostream>

int main()
{
    double r = 0, girth, area;
    const double PI = 3.14;
    std::cin >> r;
    girth = 2 * PI * r;
    area = PI * r * r;
    std::cout << girth << std::endl;
    std::cout << area << std::endl;
    while (1);
    return 0;
}

運(yùn)行的結(jié)構(gòu)如圖所示:


003.JPG

若采用C++的面向?qū)ο笤O(shè)計(jì)則程序如下:

#include<iostream>

class Circle {
    double radius;
public:
    double get_radius(double r)
    {
        radius = r;
        return 0;
    }
    double get_girth()
    {
        return 2 * 3.14*radius;
    }
    double get_area()
    {
        return 3.14*radius*radius;
    }
};
int main()
{
    Circle A;
    A.get_radius(5);
    std::cout << "The circle's girth is " << A.get_girth() << std::endl;
    std::cout << "The circle's area is " << A.get_area() << std::endl;
    while (1);
    return 0;
}

程序運(yùn)行的結(jié)構(gòu)如下:

The circle's girth is 31.4
The circle's area is 78.5

雖然此時(shí)程序稍微復(fù)雜了一點(diǎn)向楼,但是程序的復(fù)用性很高脯宿,例如可以直接計(jì)算多個(gè)圓的相關(guān)參數(shù):

int main()
{
    Circle A,B,C;
    A.get_radius(5);
    std::cout << "The circle's girth is " << A.get_girth() << std::endl;
    std::cout << "The circle's area is " << A.get_area() << std::endl;

    B.get_radius(10);
    std::cout << "The circle's girth is " << B.get_girth() << std::endl;
    std::cout << "The circle's area is " << B.get_area() << std::endl;

    C.get_radius(15);
    std::cout << "The circle's girth is " << C.get_girth() << std::endl;
    std::cout << "The circle's area is " << C.get_area() << std::endl;
    while (1);
    return 0;
}

程序的執(zhí)行結(jié)構(gòu)如下:

The circle's girth is 31.4
The circle's area is 78.5
The circle's girth is 62.8
The circle's area is 314
The circle's girth is 94.2
The circle's area is 706.5

關(guān)于公有馋袜,私有和保護(hù)成員

#include<iostream>
using namespace std;

class animal
{
public:
    char people[10];
    char lion[10];
    char glede[10];
};//類的主體是在{}中間柳击,最后需要加上一個(gè)分號(hào)

class MyClass
{
//在三者之外即未寫修飾符則默認(rèn)為私有的
//修飾符的作用域是從修飾符開始到下一個(gè)訪問修飾符或最后
public:
    //公開的弥雹,公共的
    int a = 10;
    void print();
private:
    //私有的鹦筹,類里的成員和函數(shù)才可以訪問
protected:
    //受保護(hù)的秋秤,所繼承的派生類里可以訪問
};

void MyClass::print()
{
    cout << "hello world" << endl;
}

int main()
{
    MyClass A;
    cout << A.a << endl;
    A.print();
    getchar();
    return 0;
}

程序的輸出結(jié)果如下:

10
hello world

若在私有或者保護(hù)成員里面添加項(xiàng)介衔,用對(duì)象是調(diào)用不了的恨胚。

構(gòu)造函數(shù)

class Time
{
public:
    Time();//構(gòu)造函數(shù)聲明,函數(shù)名與類名一致,沒有類型也沒有返回值
};

Time::Time()
{
    cout << "This is a constructor" << endl;
}

int main()
{
    Time t;
    getchar();
    return 0;
}

輸出結(jié)果:

This is a constructor

有參構(gòu)造

#include<iostream>
using namespace std;

class Max
{
public:
    Max(int a, int b);
//如果不屑構(gòu)造函數(shù)炎咖,編譯器會(huì)自動(dòng)生成赃泡,如果寫了,則調(diào)用你寫的構(gòu)造函數(shù)
//構(gòu)造函數(shù)分為有參和無參兩種情況乘盼,如果是有參構(gòu)造升熊,在創(chuàng)建對(duì)象的時(shí)候記得也要傳遞參數(shù)
    int MaxFunc();
private:
    int x, y;
};

Max::Max(int a, int b)
{
    cout << "This is parameter_constructor" << endl;
    x = a;
    y = b;
}

int Max::MaxFunc()
{
    return x > y ? x : y;
}
int main()
{
    Max m(3,6);
    cout << "the biggest" << m.MaxFunc() << endl;
    getchar();
    return 0;
}

程序的輸出結(jié)果是:

This is parameter_constructor
the biggest6

構(gòu)造函數(shù)的重載

#include<iostream>
using namespace std;

class Max
{
public:
    Max(int a, int b);//兩個(gè)參數(shù)的構(gòu)造函數(shù)
    Max(int a, int b, int c);//三個(gè)參數(shù)的構(gòu)造函數(shù)
    int MaxFunc1();
    int MaxFunc2();
private:
    int x;
    int y;
    int z;
};

Max::Max(int a, int b)
{
    cout << "This is two parameter_constructor" << endl;
    x = a;
    y = b;
}

Max::Max(int a, int b, int c)
{
    cout << "This is three parameter_constructor" << endl;
    x = a;
    y = b;
    z = c;
}

int Max::MaxFunc1()
{
    return x > y ? x : y;
}

int Max::MaxFunc2()
{
    return x > y ? (x > z ? x : z) : (y > z ? y : z);
}

int main()
{
    Max m1(3, 6);
    cout << "the biggest" << m1.MaxFunc1() << endl;
    Max m2(3, 6, 9);
    cout << "the biggest" << m2.MaxFunc2() << endl;
    getchar();
    return 0;
}

程序的運(yùn)行結(jié)果是:

This is two parameter_constructor
the biggest6
This is three parameter_constructor
the biggest9

拷貝構(gòu)造函數(shù)

#include<iostream>
using namespace std;

class number
{
public:
    number();//默認(rèn)構(gòu)造函數(shù)
    number(int i);//有參構(gòu)造函數(shù)
    number(number&copy);//拷貝構(gòu)造函數(shù)

    void print();//輸出函數(shù)
    void print(number obj);//重載輸出函數(shù)
private:
    int *p;
};

number::number()
{
    cout << "I am default constructor!" << endl;
}
number::number(int i)
{
    cout << "I am parameter constructor!" << endl;
    p = new int;//給指針p申請內(nèi)存
    *p = i;//形參賦值給地址p中的值
}
number::number(number&copy)//傳遞的是一個(gè)對(duì)象
{
    cout << "I am copy constructor!" << endl;
    p = new int;
    *p = *copy.p;//值拷貝
//copy是形參,*copy.p即為傳入對(duì)象的值
}

void number::print()
{
    cout << "value:" << *p << endl;
}
void number::print(number obj)
{
    cout << "value:" << *obj.p << endl;
}

int main()
{
    number n1;
    number n2(5);
    n2.print();

    number n3(10);
    n3.print();

    number n4(n2);
    n4.print();

    number n5(n3);
    n5.print();

    getchar();
    return 0;
}

程序的運(yùn)行結(jié)果是:

I am default constructor!
I am parameter constructor!
value:5
I am parameter constructor!
value:10
I am copy constructor!
value:5
I am copy constructor!
value:10

析構(gòu)函數(shù)

class MyClass
{
public:
    MyClass();//構(gòu)造函數(shù) 申請內(nèi)存 成員函數(shù) 沒有類型 沒有返回值
    ~MyClass();//析構(gòu)函數(shù) 釋放內(nèi)存 成員函數(shù) 沒有類型 沒有返回值 沒有參數(shù)
};

MyClass::MyClass()
{

}
MyClass::~MyClass()
{

}

例如在上述的程序中添加如下部分:

~number();

number::~number()
{
    cout << "I will delete the storage!" << endl;
}
//去掉getchar()

則控制臺(tái)運(yùn)行的結(jié)果變?yōu)椋?/p>

I am default constructor!
I am parameter constructor!
value:5
I am parameter constructor!
value:10
I am copy constructor!
value:5
I am copy constructor!
value:10
I will delete the storage!
I will delete the storage!
I will delete the storage!
I will delete the storage!
I will delete the storage!
請按任意鍵繼續(xù). . .

出現(xiàn)四次的原因是因?yàn)閯?chuàng)建了四次對(duì)象

友元

#include<iostream>
using namespace std;

class computer
{
public:
    void getPrice(double f);//普通成員函數(shù)绸栅,用來獲取電腦的價(jià)格
    friend void print(computer PC);//友元函數(shù)
private:
    double Price;
};

//普通成員函數(shù)级野,用來獲取電腦的價(jià)格
void computer::getPrice(double f)
{
    Price = f;
}

//友元函數(shù)不是成員函數(shù),不需要加作用域
void print(computer PC)
{
    cout << "the price is:" << PC.Price<<endl;
}

int main()
{
    computer PC;
    PC.getPrice(10000);
    print(PC);

    return 0;
}

程序運(yùn)行的結(jié)果是:

the price is:10000
請按任意鍵繼續(xù). . .

友元類

#include<iostream>
using namespace std;

class computer
{
public:
    char Name[20] = "ThinkPad";
    void getPrice(double f);
    friend class Mycomputer;
private:
    double Price=10000;
};

void computer::getPrice(double f)
{
    Price = f;
}


class Mycomputer
{
public:
    computer PC;
    void print();
};

void Mycomputer::print()
{
    cout << "My computer brand is:" << PC.Name << endl;//Name是公共的
    cout << "My computer price is:" << PC.Price << endl;
}


int main()
{
    Mycomputer pc;
    pc.print();

    return 0;
}

程序的執(zhí)行結(jié)果如下:

My computer brand is:ThinkPad
My computer price is:10000
請按任意鍵繼續(xù). . .
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末粹胯,一起剝皮案震驚了整個(gè)濱河市蓖柔,隨后出現(xiàn)的幾起案子辰企,更是在濱河造成了極大的恐慌,老刑警劉巖况鸣,帶你破解...
    沈念sama閱讀 221,273評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件牢贸,死亡現(xiàn)場離奇詭異,居然都是意外死亡镐捧,警方通過查閱死者的電腦和手機(jī)潜索,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,349評(píng)論 3 398
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來愤估,“玉大人帮辟,你說我怎么就攤上這事⊥嫜妫” “怎么了由驹?”我有些...
    開封第一講書人閱讀 167,709評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長昔园。 經(jīng)常有香客問我蔓榄,道長,這世上最難降的妖魔是什么默刚? 我笑而不...
    開封第一講書人閱讀 59,520評(píng)論 1 296
  • 正文 為了忘掉前任甥郑,我火速辦了婚禮,結(jié)果婚禮上荤西,老公的妹妹穿的比我還像新娘澜搅。我一直安慰自己,他們只是感情好邪锌,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,515評(píng)論 6 397
  • 文/花漫 我一把揭開白布勉躺。 她就那樣靜靜地躺著,像睡著了一般觅丰。 火紅的嫁衣襯著肌膚如雪饵溅。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,158評(píng)論 1 308
  • 那天妇萄,我揣著相機(jī)與錄音蜕企,去河邊找鬼。 笑死冠句,一個(gè)胖子當(dāng)著我的面吹牛轻掩,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播懦底,決...
    沈念sama閱讀 40,755評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼唇牧,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起奋构,我...
    開封第一講書人閱讀 39,660評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤壳影,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后弥臼,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體宴咧,經(jīng)...
    沈念sama閱讀 46,203評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,287評(píng)論 3 340
  • 正文 我和宋清朗相戀三年径缅,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了掺栅。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,427評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡纳猪,死狀恐怖氧卧,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情氏堤,我是刑警寧澤沙绝,帶...
    沈念sama閱讀 36,122評(píng)論 5 349
  • 正文 年R本政府宣布,位于F島的核電站鼠锈,受9級(jí)特大地震影響闪檬,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜购笆,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,801評(píng)論 3 333
  • 文/蒙蒙 一粗悯、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧同欠,春花似錦样傍、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,272評(píng)論 0 23
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至娃循,卻和暖如春炕檩,著一層夾襖步出監(jiān)牢的瞬間斗蒋,已是汗流浹背捌斧。 一陣腳步聲響...
    開封第一講書人閱讀 33,393評(píng)論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留泉沾,地道東北人捞蚂。 一個(gè)月前我還...
    沈念sama閱讀 48,808評(píng)論 3 376
  • 正文 我出身青樓,卻偏偏與公主長得像跷究,于是被迫代替她去往敵國和親姓迅。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,440評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容

  • 1.1面向?qū)ο蟮乃枷?以計(jì)算圓周長和面積的問題為例:面向過程的程序設(shè)計(jì)語言使用高級(jí)語言提供的數(shù)值計(jì)算工具,從功能入...
    蘇字龍閱讀 606評(píng)論 0 1
  • 類方法 OC 中類的方法只有類的靜態(tài)方法和類的實(shí)例方法 OC 中的方法只要聲明在 @interface 里丁存,就可以...
    devZhang閱讀 1,045評(píng)論 0 0
  • 在C++中通常有main函數(shù)以及一個(gè)或多個(gè)既包含數(shù)據(jù)成員也包含成員函數(shù)的類構(gòu)成肩杈,所以: 在一個(gè)類中,可提供一個(gè)或多...
    samtake閱讀 350評(píng)論 0 0
  • 面向?qū)ο缶幊蹋∣OP) 在前面的章節(jié)中,我們學(xué)習(xí)了Kotlin的語言基礎(chǔ)知識(shí)聋伦、類型系統(tǒng)夫偶、集合類以及泛型相關(guān)的知識(shí)。...
    Tenderness4閱讀 4,449評(píng)論 1 6
  • C++文件 例:從文件income. in中讀入收入直到文件結(jié)束觉增,并將收入和稅金輸出到文件tax. out兵拢。 檢查...
    SeanC52111閱讀 2,794評(píng)論 0 3