//程序作者:管寧
//站點:www.cndev-lab.com
//所有稿件均有版權,如要轉載,請務必聞名出處和作者
strUCt test
{
PRivate:
int number;
public:
float socre;
};
//程序作者:管寧
//站點:www.cndev-lab.com
//所有稿件均有版權,如要轉載,請務必聞名出處和作者
class test
{
private:
int number;
public:
float socre;
public:
int rp()
{
return number;
}
void setnum(int a)
{
number=a;
}
};
//程序作者:管寧
//站點:www.cndev-lab.com
//所有稿件均有版權,如要轉載,請務必聞名出處和作者
#include <iostream>
using namespace std;
class test
{
private://私有成員類外不能夠直接訪問
int number;
public://共有成員類外能夠直接訪問
float socre;
public:
int rp()
{
return number;
}
void setnum(int a)
{
number=a;
}
};
void main()
{
test a;
//a.number=10;//錯誤的,私有成員不能外部訪問
a.socre=99.9f;
cout<<a.socre<<endl;//公有成員可以外部訪問
a.setnum(100);//通過公有成員函數setnum()間接對私有成員number進行賦值操作
cout<<a.rp();//間接返回私有成員number的值
cin.get();
}
好了,介紹了在類內部定義成員函數(方法)的方法,下面我們要介紹一下域區分符(::)的作用了。
//程序作者:管寧
//站點:www.cndev-lab.com
//所有稿件均有版權,如要轉載,請務必聞名出處和作者
#include <iostream>
using namespace std;
int pp=0;
class test
{
private:
int number;
public:
float socre;
int pp;
public:
void rp();
};
void test::rp()//在外部利用域區分符定義test類的成員函數
{
::pp=11;//變量名前加域區分符給全局變量pp賦值
pp=100;//設置結構體變量
}
void main()
{
test a;
test b;
a.rp();
cout<<pp<<endl;
cout<<a.pp<<endl;
cin.get();
}
void test::rp()
{
::pp=11;
this->pp=100;//this指針就是指向a對象的指針
}
類的成員函數和普通函數一樣是可以進行重載操作的,關于重載函數前面已經說過,這里不再說明。
//程序作者:管寧
//站點:www.cndev-lab.com
//所有稿件均有版權,如要轉載,請務必聞名出處和作者
#include <iostream>
using namespace std;
class test
{
private:
int number;
public:
float socre;
int pp;
public:
void rp(int);
void rp(float);
};
void test::rp(int a)//在外部利用域區分符定義test類的成員函數
{
cout<<"調用成員函數!a:"<<a<<endl;
}
void test::rp(float a)//在外部利用域區分符定義test類的成員函數
{
cout<<"調用成員函數!a:"<<a<<endl;
}
void main()
{
test a;
a.rp(100);
a.rp(3.14f);
cin.get();
}
下面我們來看一下利用指針和利用引用間接調用類的成員函數,對于對于指針和引用調用成員函數和調用普通函數差別不大,在這里也就不再重復說明了,注重看代碼,多試多練習既可。
//程序作者:管寧
//站點:www.cndev-lab.com
//所有稿件均有版權,如要轉載,請務必聞名出處和作者
#include <iostream>
using namespace std;
class test
{
private:
int number;
public:
float socre;
int pp;
public:
int rp(int);
};
int test::rp(int a)//在外部利用域區分符定義test類的成員函數
{
number=100;
return a + number;
}
void run(test *p)//利用指針調用
{
cout<<p->rp(100)<<endl;
}
void run(test &p)//利用引用
{
cout<<p.rp(200)<<endl;
}
void main()
{
test a;
run(&a);
run(a);
cin.get();
}
class ballscore
{
protected:
const static int gbs = 5;//好球單位得分
const static int bbs = -3;//壞球單位扣分
float gradescore;//平均成績
public:
float GetGS(float goodball,float badball)//goodball為好球數量,badball為壞求數量
{
gradescore = (goodball*gbs + badball*bbs) / (goodball + badball);
return gradescore;//返回平均成績
}
};
#include <iostream>
#include "ballscore.h"
using namespace std;
void main()
{
ballscore jeff;
cout<<jeff.GetGS(10,3);
jeff.gradescore=5.5//想篡改jeff的平均成績是錯誤的!
cin.get();
}
//程序作者:管寧
//站點:www.cndev-lab.com
//所有稿件均有版權,如要轉載,請務必聞名出處和作者
#include <iostream>
using namespace std;
class ballscore
{
protected:
const static int gbs = 5;//好球單位得分
const static int bbs = -3;//壞球單位扣分
float gradescore;//平均成績
public:
float GetGS(float goodball,float badball)//goodball為好球數量,badball為壞求數量
{
int gradescore=0;//新定義一個和成員變量float gradescore相同名字的類成員函數局部變量
ballscore::gradescore = (goodball*gbs + badball*bbs) / (goodball + badball);//由于局部變量與類成員變量同名使用的時候必須在其前加上類名和域區分符
return ballscore::gradescore;//返回平均成績
}
};
int ballscore=0;//定義一個與類名稱相同的普通全局變量
int test;
void main()
{
class test//局部類的創建
{
float a;
float b;
};
test test;
::test=1;//由于局部類名隱藏了外部變量使用需加域區分符
class ballscore jeff;//由于全局變量int ballsocre和類(ballsocre)名稱相同,隱藏了類名稱,這時候定義類對象需加class前綴以區分
cout<<jeff.GetGS(10,3);
cin.get();
}
protected:
const static int gbs = 5;
const static int bbs = -3;
float gradescore;
public:
float GetGS(float goodball,float badball)
{
int gradescore=0;
ballscore::gradescore = (goodball*gbs + badball*bbs) / (goodball + badball);
return ballscore::gradescore;
}
protected:
const static int gbs = 5;
const static int bbs = -3;
float gradescore;
public:
float GetGS(float goodball,float badball)
{
int gradescore=0;
ballscore::gradescore = (goodball*gbs + badball*bbs) / (goodball + badball);
return ballscore::gradescore;
}
int test;
void main()
{
class test
{
float a;
float b;
};
test test;
::test=1;
class ballscore jeff;
cout<<jeff.GetGS(10,3);
cin.get();
}
在普通函數內部定義的類叫做局部類,代碼中的test類就是一個局部類!
|
新聞熱點
疑難解答