【C++】结构体、类和引用

C++基础与面向对象特性详解

注:最后有面试挑战,看看自己掌握了吗

结构体和类

  1. C++结构体中可以有函数
  2. 称为成员函数
#include <iostream>
struct point
{
   
   
	int x;
	int y;
	void output()
	{
   
   
		std::cout<<x<<std::endl<<y;
	}
};

void main()
{
   
   
	point pt;
	pt.x=0;
	pt.y=0;
	pt.output();
}
  1. C++中,结构体使用关键字struct声明的类
  2. 差异:结构体默认成员公有public
  3. :默认成员为private

构造函数

  1. 构造函数名字一定和类名相同
#include <iostream>
using namespace std;
class point
{
   
   
public:
	int x;
	int y;
	point()
	{
   
   
		x=0;
		y=0;
	}
	point(int a,int b)
	{
   
   
		x=a;
		y=b;
	}
	void output()
	{
   
   
		cout<<x<<endl<<y<<endl;
	}
};
void main()
{
   
   
	point pt(5,5);
	pt.output();
	point cc
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

扯淡散人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值