資料成員、設定函數及擷取函數

來源:互聯網
上載者:User

局部變數--聲明在一個函數定義的體中的變數。

函數中的局部變數必須在使用前聲明,並且不能再聲明它們的函數外部被訪問。

當函數結束時,其局部變數的值將丟失。

 

 

類通常由一個或多個成員函數組成,這些成員函數操縱屬於該類的某個特定對象的屬性。在類的定義中屬性工作表示為變數。這樣的變數稱為資料成員,聲明在類的定義之內,但是在所有類的成員函數的定義體之外。

2010-01-08

 

#include "stdafx.h"

#include "iostream"
using std::cout;
using std::cin;
using std::endl;

#include "string"
using std::string;
using std::getline;

class GradeBook
{
public:
 void setCourseName( string name )
 {
  courseName = name;
 }

 string getCourseName()
 {
  return courseName;
 }

 void displayMessage()
 {
  cout << "Welcome to the grade book for/n" << getCourseName() << "!" << endl;
 }

private:
 string courseName;
}; //end class

int main()
{
 string nameOfCourse;
 GradeBook myGradeBook;

 cout << "Inital Course name is: " << myGradeBook.getCourseName() << endl;

 cout << "/n Please enter the course name:" << endl;
 getline( cin, nameOfCourse );
 myGradeBook.setCourseName( nameOfCourse );

 cout << endl;
 myGradeBook.displayMessage();
 return 0;
}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.