//程序作者:管宁
//站点: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类就是一个局部类! 新闻热点
疑难解答