C++关于类的大小、与class的区别等问题详解(附代码)

目录

1.类的定义

2.类的大小

3.访问权限

4.struct 和class的区别

5.面向对象的特点:

6.类中隐藏的this指针

7.类中的函数重载

8.带默认值的函数注意

9.C和C++中struct的区别


1.类的定义

抽象出来的,将现实生活中实实在在的一些实体的共性抽象出来,形成一个类型将不同类型的数据以及与这些数据相关的操作封装在一起,构成一个新的数据类型,实现不占用内存单元将不同类型的数据以及这些数据相关的操作封装在一起
属性+函数

class Clock//类名,一般大写
{
public:
	//带默认值的函数(带缺省值的函数),可替代重载函数
	void Set(int h = 12, int m = 30, int s = 30)
	{
		m_hour = h;
		m_minute = 20;
		m_second = 30;
	}
	void Show()//显示时间
	{
		cout << m_hour << ":" << m_minute << ":" << m_second << endl;
	}
protected:
	int m_test;
private:
	int m_hour;//m_表示成员的意思
	int m_minute;
	int m_second;
};

2.类的大小

1.一般情况下是属性之和,普通函数(在代码区)不占类空间大小
2.static属性不占类空间大小
3.virtual虚函数在类中分配一个指针大小,不管有多少个虚函数都只一个指针大小//原因:虚指针指向虚表入口,不管有多少都指向这一张表的入口

class AA
{
public:
	void test() {}
	virtual void fn() {}
	virtual void fn1() {}
	virtual void fn2() {}
private:
	int m_i;
	char m_sex;
	int m_age;
	//static int m_num;
};

void main()
{
	cout << sizeof(AA) << endl;
}

3.访问权限

访问权限--public,protected,private
在继承之前protected可以不用

class Clock//类名,一般大写
{
public:
	void Set()//设置时间
	{
		m_hour = 02;
		m_minute = 11;
		m_second = 09;
	}
	void Show()//显示时间
	{
		cout << m_hour << ":" << m_minute << ":" << m_second << endl;
	}
protected:
	int m_test;
private:
	int m_hour;//m_表示成员的意思
	int m_minute;
	int m_second;
};
void main()
{
	Clock c;//Clock类定义了一个名为c的对象
	//c.m_test = 10;//在外界不能访问保护和私有的
	//c.m_hour = 20;//在外界不能访问保护和私有的
	c.Set();
	c.Show();
}

4.struct 和class的区别

 struct 的默认权限是public
 class  的默认权限是private

5.面向对象的特点:

封装 继承 多态;抽象
封装--属性(private)+操作(public)
继承--上层和下层关系
多态--多种状态或形态
    4种多态
        1.重载多态:函数重载//函数名相同参数列表不同,通过返回值不能确定重载
                             运算符重载
        2.强制多态:强制类型转换 static_cast,dynamic_cast,const_cast,reinterpret_cast
        3.包含多态:virtual--虚函数
        4.参数多态:模板

struct AA
{
	void print() { cout << "AA:print" << endl; }
};

class BB
{
	void print() {}
};

void main()
{
	AA a;
	BB b;
	a.print();
	//b.print;
}

6.类中隐藏的this指针

this--在非(static)静态成员函数中,有一个隐含的指针this,this指向当前类对象,即是接收当前类对象地址 this=&当前对象

class Clock
{
public:
	void Set(int h)//c1.Set(12) c1.Set(&c1,12)
	{//this=&当前对象
		this->m_hour = h;
		this->m_minute = 20;
		this->m_second = 30;
	}
	void Show()//显示时间
	{
		cout << this->m_hour << ":" << this->m_minute << ":" << this->m_second << endl;
	}
protected:
	int m_test;
private:
	int m_hour;
	int m_minute;
	int m_second;
};

void main()
{
	Clock c1, c2;
	c1.Set(12);
	c1.Show();
	c2.Set(9);
	c2.Show();

}

7.类中的函数重载

        1.函数名相同(同一个作用域)
        2.函数的参数列表不同(参数的类型,个数,顺序)
        3.和函数返回值无关
        4.和常成员函数无关

class Clock
{
public:
	void Set()
	{
		m_hour = 1;
		m_minute = 20;
		m_second = 30;
	}
	void Set(int h)
	{
		this->m_hour = h;
		this->m_minute = 20;
		this->m_second = 30;
	}
	void Set(int h, int m)
	{
		m_hour = h;
		m_minute = m;
		m_second = 30;
	}
	void Set(int h, int m, int s)
	{
		m_hour = h;
		m_minute = m;
		m_second = s;
	}
	void Show()//显示时间
	{
		cout << this->m_hour << ":" << this->m_minute << ":" << this->m_second << endl;
	}
protected:
	int m_test;
private:
	int m_hour;
	int m_minute;
	int m_second;
};
void Clock::Show()
{
	cout << this->m_hour << ":" << this->m_minute << ":" << this->m_second << endl;
}

//void ::Show()//不能重载,函数名不同
void Set(int h, int m, int s, int f)
{
}

void main()
{
	Clock c, c1, c2, c3;
	c.Set();
	c.Show();
	c1.Set(12);
	c1.Show();
	c2.Set(9,11);
	c2.Show();
    c3.Set(4,5,6);
	c3.Show();    

}

8.带默认值的函数注意

        1.一般情况下,尽量参数都带默认值
               如果有不带的,一定在前面,即是有一个参数带默认值了,它后面的必须带默认值
        2.在声明的时候带默认值,如果放在类外定义此函数,则默认值不需要再定义

class A
{
public:
	void fn(int i = 0, int j = 0, int k = 0)//(缺省函数)
	{
		m_i = i;
		m_j = j;
		m_k = k;
	}
	void Print() { cout << m_i << " " << m_j << " " << m_k << endl; }//隐式内联
private:
	int m_i;
	int m_j;
	int m_k;
	/*void A::fn(int i = 0, int j = 0, int k = 0)
		{
			m_i = i;
			m_j = j;
			m_k = k;
		}*/
};

void main()
{
	A a, b, c, d;
	a.fn();
	b.fn(1);
	c.fn(1, 2);
	d.fn(1, 2, 3);
	a.Print();
	b.Print();
	c.Print();
	d.Print();
}

9.C和C++中struct的区别
*    1.C中不可以为空,C++可以为空,大小为一(占位符)
*    2.struct student  C中student是结构体名 C++类型名
*    3.函数 C中不能出现函数成员,C++可以出现函数
*    4.C语言函数名不能相同 C++可以

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值