C++学习一
2015-06-03 12:20
411 查看
#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;
}
#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;
}
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- 关于指针的一些事情
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- Lua教程(一):在C++中嵌入Lua脚本
- Lua教程(二):C++和Lua相互传递数据示例
- C++联合体转换成C#结构的实现方法
- C++编写简单的打靶游戏
- C++变位词问题分析
- C/C++数据对齐详细解析
- C++基于栈实现铁轨问题
- C++中引用的使用总结
- C++中调用Lua函数实例
- Lua和C++的通信流程代码实例
- C与C++之间相互调用实例方法讲解
- C++中拷贝构造函数的应用详解
- C++中引用(&)的用法与应用实例分析
- C++使用CriticalSection实现线程同步实例
- C++智能指针实例详解
- 解析C++ 浮点数的格式化输出