C++面向对象模型初探

来源:互联网 发布:杭州淘宝客服真实工资 编辑:程序博客网 时间:2024/06/01 10:15

前言

C++对象模型可以概括为以下2部分:

1. 语言中直接支持面向对象程序设计的部分,主要涉及如构造函数、析构函数、虚函数、继承(单继承、多继承、虚继承)、多态等等。

2. 对于各种支持的底层实现机制。

在c语言中,“数据”和“处理数据的操作(函数)”是分开来声明的,也就是说,语言本身并没有支持“数据和函数”之间的关联性。在c++中,通过抽象数据类型(abstractdata type,ADT),在类中定义数据和函数,来实现数据和函数直接的绑定

概括来说,在C++类中有两种成员数据:static、nonstatic;三种成员函数:static、nonstatic、virtual。




基础知识

#include "iostream"using namespace std;class C1{public:int i;  //4int j; //4int k;  //4protected:private:}; //12class C2{public:int i; int j; int k; static int m; //4public:int getK() const { return k; } //4void setK(int val) { k = val; }  //4protected:private:}; //24 16 12(铁钉的不对)struct S1{int i;int j;int k;}; //12struct S2{int i;int j;int k;static int m;}; //16int main()                                          结果:12 12 12 12{printf("c1:%d \n", sizeof(C1));printf("c2:%d \n", sizeof(C2));printf("s1:%d \n", sizeof(S1));printf("s2:%d \n", sizeof(S2));system("pause");}

通过上面的案例,我们可以的得出:


1)C++类对象中的成员变量和成员函数是分开存储的

成员变量:

普通成员变量:存储于对象中,与struct变量有相同的内存布局和字节对齐方式

静态成员变量:存储于全局数据区中

成员函数:存储于代码段中。


2)问题出来了:很多对象共用一块代码?代码是如何区分具体对象的那?

换句话说:int getK() const { return k; },代码是如何区分,具体obj1、obj2、obj3对象的k

this指针


#include <iostream>using namespace std;class Test{public:Test(int a, int b) //---> Test(Test *this, int a, int b){this->a = a;this-> b = b;}void printT(){cout<<"a: " <<a <<endl;cout<< "b: " << this->b <<endl;}//1 const 写的什么位置 没有关系 //2 const修饰的是谁?// 2-1const修饰的是形参a 不是// 2-2const修饰的是属性this->a  this->b // 2-3 const修饰的是this指针所指向的内存空间, 修饰的是this指针 void  OpVar( int a, int b) const   //==>void  OpVar(const Test *this, int a, int b)   //==>void  OpVar( const Test *const this, int a, int b)  {a = 100;//this->a = 100;//this->b = 200;//this = 0x11;//cout<<"a: " <<a <<endl;cout<< "b: " << this->b <<endl;}protected:private:int a;int b;};void main(){int *m_space = new int[0];if (m_space == NULL){return ;}Test t1(1, 2);t1.printT();// ===> printT(&t1)cout<<"hello..."<<endl;system("pause");return ;}


在设计类的时候,一个原则就是对于不改变数据成员的成员函数都要在后面加 const,而对于改变数据成员的成员函数不能加 const。所以 const 关键字对成员函数的行为作了更加明确的限定:有 const 修饰的成员函数(指 const 放在函数参数表的后面,而不是在函数前面或者参数表内),只能读取数据成员,不能改变数据成员;没有 const 修饰的成员函数,对数据成员则是可读可写的。

除此之外,在类的成员函数后面加 const 还有什么好处呢?那就是常量(即 const)对象可以调用 const 成员函数,而不能调用非const修饰的函数。


全局函数PK成员函数

1、把全局函数转化成成员函数,通过this指针隐藏左操作数

                   Testadd(Test &t1, Test &t2)===》Test add(Test&t2)

         2、把成员函数转换成全局函数,多了一个参数

                   voidprintAB()===》void printAB(Test *pthis)

         3、函数返回元素和返回引用

                            Test& add(Test &t2) //*this //函数返回引用

                            {

                                      this->a = this->a +t2.getA();

                                      this->b = this->b +t2.getB();

                                     return*this; //*操作让this指针回到元素状态

                            }

 

                            Test add2(Test &t2)//*this //函数返回元素

                            {

                                     //t3是局部变量

                                      Test t3(this->a+t2.getA(),this->b + t2.getB()) ;

                                      return t3;                           //会调用copy构造函数,而且此匿名对象有可能被析构掉,之所以是有可能,还是要看是赋值还是初始化

                            }





原创粉丝点击