C++学习一

#include <iostream>
#include <assert.h>


using namespace std;


/*
第一:private, public, protected 访问标号的访问范围。
private:只能由1.该类中的函数、2.其友元函数访问。
不能被任何其他访问,该类的对象也不能访问。


protected:可以被1.该类中的函数、2.子类的函数、以及3.其友元函数访问。
但不能被该类的对象访问。


public:可以被1.该类中的函数、2.子类的函数、3.其友元函数访问,也可以由4.该类的对象访问。
 
注:友元函数包括3种:设为友元的普通的非成员函数;设为友元的其他类的成员函数;设为友元类中的所有成员函数。
---------------------------------------------------------------------------------------------------------------
1.类的一个特征就是封装,public和private作用就是实现这一目的。所以:
用户代码(类外)可以访问public成员而不能访问private成员;private成员只能由类成员(类内)和友元访问。


2.类的另一个特征就是继承,protected的作用就是实现这一目的。所以:
protected成员可以被派生类对象访问,不能被用户代码(类外)访问。


继承中的特点:
先记住:不管是否继承,上面的规则永远适用!
*/
class A
{
public:
int a;
A()
{
a1 = 1;
a2 = 2;
a3 = 3;
a = 4;
}
void fun()
{
cout<<"fun---A---in"<<endl;
cout<<a<<endl;
cout<<a1<<endl;// internal access in class wheather protected, private,public
cout<<a2<<endl;
cout<<a3<<endl;
cout<<"fun---A--out\n"<<endl;
}
void changevalue(int a)
{
a2 = a2 + a;
a3 = a3 + 2*a;
}
public:
int a1;
protected:
int a2;
private:
int a3;
};


class B:public A
{
public:
int a;
B(int i)
{
A();
a = 1;
}
void fun()
{
cout<<"fun---B---in"<<endl;
cout<<a<<endl;
cout<<a1<<endl;
//a2说明子类可以访问父类的protected 数据信息
cout<<a2<<endl;// shows the object of protected can be accessed by subclass fun, not by subclass object
//cout<<a3<<endl;// a3 is also the private data
cout<<"fun---B---out"<<endl<<endl;
}
};


class C:protected A
{
public:
int a;
C(int i)
{
A();
a = 1;
}
void fun()
{
cout<<"fun---C---in"<<endl;
cout<<a<<endl;
cout<<a1<<endl;
cout<<a2<<endl;// access by subclass fun, not by subclass object
//cout<<a3<<endl;
//cout<<a3<<endl;// a3 is also the private data
cout<<"fun---C---out\n"<<endl;
}
void changevalue(int a)
{
a = a + a;
a1 = a1 + 2 * a;
a2 = a2 + 3 * a;
//a3 = a3 + 4 * a;
}
};


class D:private A
{
public:

D(int i)
{
A();

}
void fun()
{
cout<<"fun---D---in"<<endl;
cout<<a<<endl;
cout<<a1<<endl;
//here the access to a2 shows that whatever which one inherit  can access the protected objects of parent class
cout<<a2<<endl;// access by subclass fun, not by subclass object
//cout<<a3<<endl;// a3 is also the private data
cout<<"fun---D---out\n"<<endl;
}
};




int main()
{
A itema;
B b(10);
C c(11);
D d(12);
itema.fun();//default value
itema.a = 10;//public a is accessed by class object
itema.a1 = 20;// public a1, the same to a
itema.fun();//a and a1 have been changed
itema.changevalue(100);
itema.fun();// a2 and a3 been amended by the class function, showing only class funciton can access the private and protected object
//itema.a2 = 30;// a2 and a3 show that class's protected and private data are not accessed by class object itema
//itema.a3 = 40;//说明private 和 protected 数据信息只能被该类本身的成员函数访问,而不能被该类的对象访问


// 
cout<<"class B begining----"<<endl;
b.fun();//if subclass has the same function in itself, the subclass function will cover the original class funciton, so here class B's function will be
// excuted, not only the class A's
cout<<"b.a-----"<<b.a<<endl;
cout<<"b.a1-----"<<b.a1<<endl;
cout<<"class B end"<<endl<<endl;
//cout<<"b.a2-----"<<b.a2<<endl;// b is the object of derivation, so it can't access the protected object of a2 which localed in parent class
//cout<<"b.a3-----"<<b.a3<<endl; a2 and a3 can't be accessed by subclass's object
b.changevalue(222);//change the parent class private object's value through calling the method of parent class
b.fun();// b element function can access the protected data inherited from parent class 


// 
c.fun();
cout<<"c.a-----"<<c.a<<endl;
//cout<<"c.a1"<<c.a1<<endl; // a1 is not accessed by the object of subclass C, a1 is a protected object in subclass C, so only method of subclass C can access a1
c.changevalue(100);
cout<<"c.fun------"<<endl;
c.fun();
//cout<<"c.a1-----"<<c.a1<<endl;// protected data is not accessed by the object c 
//cout<<"b.a2-----"<<b.a2<<endl;
//cout<<"b.a3-----"<<b.a3<<endl; a2 and a3 can't be accessed by subclass's object




// 
d.fun();
//cout<<"d.a-----"<<d.a<<endl;// here subclass D has not the data a, so according to the regular, the object of subclass can't access the a
//cout<<"c.a1-----"<<c.a1<<endl;// protected data is not accessed by the object c 
//cout<<"b.a2-----"<<b.a2<<endl;
//cout<<"b.a3-----"<<b.a3<<endl; a2 and a3 can't be accessed by subclass's object


//
cout<<"A size-----"<<sizeof(A)<<endl;
cout<<"B size-----"<<sizeof(B)<<endl;
cout<<"C size-----"<<sizeof(C)<<endl;
cout<<"D size-----"<<sizeof(D)<<endl;
system("pause");
return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值