面向对象模型初探
1.基础知识
C++对象模型可以概括为以下2部分:
1. 语言中直接支持面向对象程序设计的部分,主要涉及如构造函数、析构函数、虚函数、继承(单继承、多继承、虚继承)、多态等等。
2. 对于各种支持的底层实现机制。
在c语言中,“数据”和“处理数据的操作(函数)”是分开来声明的,也就是说,语言本身并没有支持“数据和函数”之间的关联性。在c++中,通过抽象数据类型(abstract data type,ADT),在类中定义数据和函数,来实现数据和函数直接的绑定。
概括来说,在C++类中有两种成员数据:static、nonstatic;三种成员函数:static、nonstatic、virtual。
C++中的class从面向对象理论出发,将变量(属性)和函数(方法)集中定义在一起,用于描述现实世界中的类。从计算机的角度,程序依然由数据段和代码段构成。
C++编译器如何完成面向对象理论到计算机程序的转化?
换句话:C++编译器是如何管理类、对象、类和对象之间的关系
具体的说:具体对象调用类中的方法,那,c++编译器是如何区分,是那个具体的类,调用这个方法那?其实都是通过编译器进行词法分析完成的!
另外为了证明static属性的成员变量存放在全局数据区,以及所有的成员函数都是存放在代码段,补充如下代码:
#include "iostream"
#include <stdlib.h>
#include <stdio.h>
using namespace std;
class C1
{
public:
int i; //4
int j; //4
int k; //4
protected:
private:
}; //12
class C2
{
public:
int i;
int j;
int k;
static int m; //4
public:
int getK() const { return k; } //4
void setK(int val) { k = val; } //4
protected:
private:
}; //24 16 12(铁钉的不对)
struct S1
{
int i;
int j;
int k;
}; //
struct S2
{
int i;
int j;
int k;
static int m;
}; //
int main()
{
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");
return 0;
}
2.编译器对属性和方法的处理机制
通过上面的案例,我们可以的得出:
1)C++类对象中的成员变量和成员函数是分开存储的
成员变量:
普通成员变量:存储于对象中,与struct变量有相同的内存布局和字节对齐方式
静态成员变量:存储于全局数据区中
成员函数:存储于代码段中。
问题出来了:很多对象共用一块代码?代码是如何区分具体对象的那?
换句话说:int getK() const { return k; },代码是如何区分,具体obj1、obj2、obj3对象的k值?
2)C++编译器对普通成员函数的内部处理
总结:
1、C++类对象中的成员变量和成员函数是分开存储的。C语言中的内存四区模型仍然有效!
2、C++中类的普通成员函数都隐式包含一个指向当前对象的this指针。
3、静态成员函数、成员变量属于类
静态成员函数与普通成员函数的区别
静态成员函数不包含指向具体对象的指针
普通成员函数包含一个指向具体对象的指针
3.this指针
实验1:若类成员函数的形参 和 类的属性,名字相同,通过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;
}
protected:
private:
int a;
int b;
};
void main()
{
Test t1(1, 2);
t1.printT();// ===> printT(&t1)
cout<<"hello..."<<endl;
system("pause");
return ;
}
实验2:类的成员函数可通过const修饰,请问const修饰的是谁?
答:修饰的是this指针指向的内存空间,且this指针即使没有const修饰成员函数也不可修改其值。
#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;
};
int main()
{
int *m_space = new int[0];
if (m_space == NULL)
{
return 1 ;
}
cout<<m_space<<endl;
Test t1(1, 2);
t1.printT();// ===> printT(&t1)
cout<<"hello..."<<endl;
system("pause");
return 1;
}
4.全局函数PK成员函数
- 把全局函数转化成成员函数,通过this指针隐藏左操作数
Test add(Test &t1, Test &t2)===》Test add(Test &t2)
- 把成员函数转换成全局函数,多了一个参数
void printAB()===》void printAB(Test *pthis)
- 函数返回元素和返回引用
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;
}
重要示例代码
#include <iostream>
using namespace std;
class Test
{
public:
int a;
int b;
public:
~Test()
{
cout<<"a:"<<a<<" b: "<<b;
cout<<"\tdestructor"<<endl;
}
public:
void printT()
{
cout<<"a:"<<a<<" b: "<<b<<endl;
}
public:
//t3 = t1.TestAdd(t2);
Test TestAdd(Test &t2)
{
Test tmp(this->a + t2.a, this->b + t2.b);
return tmp;
}
//t1.TestAdd2(t2);
//返回一个引用 相当于返回自身
//返回t1这个元素 this就是&t1
Test& TestAdd2(Test &t2)
{
this->a = this->a + t2.a;
this->b = this->b + t2.b;
return *this; //把 *(&t1) 又回到了 t1元素
}
public:
Test(int a=0, int b=0)
{
this->a = a;
this->b = b;
}
};
//把成员函数 转成 全局函数 多了一个参数
void printT(Test *pT)
{
cout<<"a:"<<pT->a<<" b: "<<pT->b<<endl;
}
//全局函数的方法
//全局函数 转成 成员函数 少了一个参数
Test TestAdd(Test &t1, Test &t2)
{
Test tmp;
return tmp;
}
int main()
{
Test t1(1, 2);
Test t2(3, 4);
Test t3 ;
//全局函数方法
t3 = TestAdd(t1, t2);//第一次析构(全局函数返回的匿名对象用于赋值操作不是初始化)
//成员函数方法
{
//先把测试案例写出来
Test t4 = t1.TestAdd(t2); //匿名对象直接转化成t4,不会被析构
t4.printT();
Test t5;
t5 = t1.TestAdd(t2); //第二次析构,匿名对象 复制 给t5,会被析构
t5.printT();
//在跳出改语句块之前,第三次析构(t5),第四次析构(t4)
}
Test t6(1, 2);
Test t7(3, 4);
//t1 = t1 + t2;
t6.TestAdd2(t7);//返回引用不会被析构,但引用代表的变量已被修改
t6.printT();
//结束main函数之前,第五次析构(t7),第六次析构(t6),第七次析构(t3),第八次析构(t2),第九次析构(t1)
return 1;
}