程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> C++學習-類和對象(2)

C++學習-類和對象(2)

編輯:C++入門知識

類(class)是C++面向對象程序設計的核心,它是實現抽象類型的工具。類是通過抽象數據類型的方法來實現的一種數據類型。類是對某一類對象的抽象,而對象是某一種類的實例。
本文稍微說一下類和對象的基本使用,比較簡單也是最基本的C++基礎。
一. 類
類的定義有2個部分:聲明部分和實現部分。
聲明部分:用來聲明類中的數據成員和成員函數(方法)。 ->告訴用戶"干什麼?"。
實現部分:用來對成員函數的定義。 ->告訴用戶"怎麼干"。

 


二. 類的關鍵字

1.public
公有成員,在程序的任何地方都可以被訪問。
2.private
私有成員,只能被成員函數和類的友元訪問。
3.protected
保護成員,用於繼承中。

 


三. 類的聲明

下面只提供最簡單的類聲明,因為一個真正的類,還會涉及到很多性質或者特點。
本實驗是在Code::Blocks工具中進行的,為了規范性,這裡總共創建了3個源文件:main.cpp test1.cpp test1.h
main.cpp: 主程序
test1.cpp: test1.h頭文件中的類的定義

test1.h: 類的聲明


[cpp] //test1.h  
#ifndef TEST1_H_INCLUDED  
#define TEST1_H_INCLUDED  
 
class CAnimal 

public: 
    int getAge(); 
    void setAge(int age); 
    void addAge(); 
 
private: 
    int mAge; 
}; 
 
#endif // TEST1_H_INCLUDED 
//test1.h
#ifndef TEST1_H_INCLUDED
#define TEST1_H_INCLUDED

class CAnimal
{
public:
    int getAge();
    void setAge(int age);
    void addAge();

private:
    int mAge;
};

#endif // TEST1_H_INCLUDED


四. 類的定義
當類的成員函數的函數體在類的外部定義時,必須由作用域運算符"::"來通知編譯系統該函數所屬的類。

 

[cpp] //test1.cpp  
#include "test1.h"  
 
int CAnimal::getAge() 

    return mAge; 

 
void CAnimal::addAge() 

    mAge++; 

 
void CAnimal::setAge(int age) 

    mAge = age; 

//test1.cpp
#include "test1.h"

int CAnimal::getAge()
{
    return mAge;
}

void CAnimal::addAge()
{
    mAge++;
}

void CAnimal::setAge(int age)
{
    mAge = age;
}


五. 對象的定義
CAnimal類的概念已經創建出來,現在就要實例化出一只動物,這裡就用阿貓阿狗來舉例。以下用2種方式來實例化對象:
1.CAnimal dog; //創建出了一只dog
2.CAnimal *cat = new CAnimal; //動態創建了一只cat
以下貼出使用dog和cat的具體操作:

 

[cpp] //main.cpp  
#include <iostream>  
#include "test1.h"  
 
using namespace std; 
 
int main() 

    //dog  
    CAnimal dog; 
 
    dog.setAge(12); 
    cout << "Dog is " << dog.getAge() << " years old." << endl; 
 
    cout << "After one year." << endl; 
 
    dog.addAge(); 
    cout << "Dog is " << dog.getAge() << " years old." << endl; 
 
    //cat  
    CAnimal *cat = new CAnimal; 
    cat->setAge(12); 
    cout << "Cat is " << cat->getAge() << " years old." << endl; 
 
    cout << "After one year." << endl; 
 
    cat->addAge(); 
    cout << "Cat is " << cat->getAge() << " years old." << endl; 
 
    return 0; 

//main.cpp
#include <iostream>
#include "test1.h"

using namespace std;

int main()
{
    //dog
    CAnimal dog;

    dog.setAge(12);
    cout << "Dog is " << dog.getAge() << " years old." << endl;

    cout << "After one year." << endl;

    dog.addAge();
    cout << "Dog is " << dog.getAge() << " years old." << endl;

    //cat
    CAnimal *cat = new CAnimal;
    cat->setAge(12);
    cout << "Cat is " << cat->getAge() << " years old." << endl;

    cout << "After one year." << endl;

    cat->addAge();
    cout << "Cat is " << cat->getAge() << " years old." << endl;

    return 0;
}


六. 程序運行結果

 

[plain] Dog is 12 years old. 
After one year. 
Dog is 13 years old. 
Cat is 12 years old. 
After one year. 
Cat is 13 years old. 
Dog is 12 years old.
After one year.
Dog is 13 years old.
Cat is 12 years old.
After one year.
Cat is 13 years old.


七. C++中class與struct的區別
在C++面向對象語言中,class和struct的功能很相似,struct也是一種類。
區別:
一個struct也是一個class,但是struct的默認方式是公用部分,而class的默認方式是私用部分。
以下使用代碼說明,下面的class與struct是等價的:

 

[cpp] class: 
class CAnimal 

    //默認是private  
    int mAge; 
public: 
    int getAge(); 
    void setAge(int age); 
    void addAge(); 
}; 
<=> 
struct: 
struct CAnimal 

    //默認是public  
    int getAge(); 
    void setAge(int age); 
    void addAge(); 
private: 
    int mAge; 
}; 
class:
class CAnimal
{
    //默認是private
    int mAge;
public:
    int getAge();
    void setAge(int age);
    void addAge();
};
<=>
struct:
struct CAnimal
{
    //默認是public
    int getAge();
    void setAge(int age);
    void addAge();
private:
    int mAge;
};


八. 為什麼類的定義以分號結束
這是什麼問題?有人說我很無聊。
其實並不單單類的定義要以分號結束,C++中的struct也要以分號結束。原因很簡單,因為咱們的使用習慣,或者說是編譯器編譯程序要考慮的情況,在定義之後,可以接一個對象的定義列表,理所當然要以分號結束。
看幾個簡單的例子:

1.class


[cpp] class CAnimal 

//...  
}dog, cat; 
class CAnimal
{
//...
}dog, cat;

2.struct


[cpp] struct CAnimal 

//...  
}dog, cat; 
struct CAnimal
{
//...
}dog, cat;

 

3.int a, b, c; //O(∩_∩)O哈哈~

本博文是非常基礎的,由簡單循序漸進到復雜,這樣才能培養出興趣與恆心!

摘自 gzshun的專欄

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved