【知识学习】C++的类 及与 结构体的区别

推荐(收藏)一篇博客:https://blog.csdn.net/u011555996/article/details/102584673

结构体默认是public的 类有public和private之分

主要还是说类吧(结构体好像是C里面用的多吧)

类包含的函数啊 还有private和public体现在哪啊 都在注释和上面博文中有体现

好了 直接看代码吧

#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<string>
#include<algorithm> 
#include<cmath>
#include<vector>
#include<map>
using namespace std;
typedef long long LL;
#define PI acos(-1)
#define fin freopen("data.txt","r",stdin)
#define INF 2147483647
#define eps 1e-7
#define L 100005
#define Fo(i,a,b) for(LL i=(a),_=(b); i<=_; i++)
#define Ro(i,b,a) for(LL i=(b),_=(a); i>=_; i--)
#define Ms(a,b) memset((a),(b),sizeof(a))
#define _ceil(_,__) (_+(__-1))/__
#define debug(_) cout<<endl<<"d::"<<_<<endl<<endl
#define type(_) typeid(_).name()
inline LL read() {
	LL x = 0, f = 1;char c = getchar();
	while (!isdigit(c)) { if (c == '-')f = -f;c = getchar(); }
	while (isdigit(c)) x = (x << 1) + (x << 3) + (c ^ 48ll), c = getchar();
	return x * f;
}

struct Stu1 { //都是public
	string name = "Zhang San";
	int mark = 100;
	Stu1(string s, int m) {
		name = s;
		mark = m;
	}
	Stu1(const Stu1& s) {
		name = s.name;
		mark = s.mark;
	}
	string getname() const {
		return name;
	}
	int getmark() const {
		return mark;
	}
	~Stu1() {
		cout << "Destructor of Struct" << endl;
	}
};

class Stu2 { //不只有public 还有private
private: //数据成员
	string name = "Zhang San"; //赋默认值
	int mark = 100;
public:
	Stu2(string s, int m) { //默认构造函数
		name = s;
		m = mark;
	}
	Stu2(const Stu2& s) { //复制构造函数
		name = s.name;
		mark = s.mark;
	}
	string getname() const { 
		return name;
	}
	int getmark() const{
		return mark;
	}
	~Stu2() { //析构函数
		cout << "Destructor of Class" << endl;
	}
};

int main() {
	Stu1 a("Li Si", 60);
	Stu2 b("Cls", 60);
	cout << a.name << " " << a.getname() << endl;
	cout << b.getname(); //这里就不能写b.name
	return 0;
}

待更,没完事儿呢。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

cls1277

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

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

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

打赏作者

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

抵扣说明:

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

余额充值