C++类class、结构体struct

1.类为class,对象是类class定义的变量。
猫(类),胖橘(对象)。

class cat{

};
cat fat_ju;

女朋友(类),沙雕(对象)。

class gf{

};
gf _250;
typedef struct MYTIME
{
	int hour;//0-24
	int min;//0-60
	MYTIME(){}//默认构造函数
	MYTIME(int min,int hour):min(min),hour(hour){}//有参构造
}MyTime;

2.结构体为struct。

struct cat{

};

一.struct和class的区别

例子1:

#include <iostream>
using namespace std;
//结构体
struct hero
{
	char name[60];
	int sex;
};
//注意此函数在类中使用
void printhero(struct hero& p) {//&引用,非常好用。
	cout <<"结构体:" << "名字:" << p.name << ",性别:" << p.sex << endl;
}
//类,申请的变量称为对象
class super_hero
{
int hight;//默认为private:
public://访问控制权限
	char name[60];
	int sex;
	//此处因为是在类中定义,没有必要传参
	void printhero() {
		cout <<"类:" << "名字:" << name << ",性别:" << sex << endl;
	}
};
int main(int argc, char** argv) {
	//结构体使用
	hero h1;
	strcpy_s(h1.name, "盖伦");
	h1.sex = 1;
	printhero(h1);
	//类使用
	super_hero sh1;//类  对象
	strcpy_s(sh1.name, "春哥");
	sh1.sex = 1;
	sh1.printhero();
}

结果:
在这里插入图片描述
例子2:
struct方式:

struct date
{
	int year;
	int month;
	int day;
};
void init_date(struct date& d) {
	cout << "请输入:"<<endl;
	cin >> d.year >> d.month >> d.day;
}
void print_date(struct date& d) {
	cout << "year:" << d.year << ",month:" << d.month << ",day:" << d.day << endl;
}
string is_good_day(struct date& d) {
	if (d.month == 6 && d.day == 8)
		return "good";
	return "bad";
}
int main(int argv, char** argc) {
	date d;
	init_date(d);
	print_date(d);
	cout << "is run year?" << is_good_day(d) << endl;
}

结果:
在这里插入图片描述
class改写struct:

#include <iostream>
using namespace std;

class date
{
public:
	int year;
	int month;
	int day;
	void init_date() {
		cout << "请输入:" << endl;
		cin >> year >> month >> day;
	}
	void print_date() {
		cout << "year:" << year << ",month:" << month << ",day:" << day << endl;
	}
	string is_good_day() {
		if (month == 6 && day == 8)
			return "good";
		return "bad";
	}
};
int main(int argv, char** argc) {
	date d;
	d.init_date();
	d.print_date();
	cout << "is run year?" << d.is_good_day() << endl;

}

结果:
在这里插入图片描述

二.class中private、public、protected区别,类的封装使用

在这里插入图片描述public:可以在类外进行赋值、使用。
private:不能在类外进行赋值、使用,必须通过定义类内public下的方法,才能调用。
protected:保护控制权限,在类的继承中跟private有区别,在单个类中,跟private是一摸一样的。

#include <iostream>
using namespace std;

class animal {
	int hight;//默认为private;
public:
	//在public下面定义的成员变量和函数,可以在类的内部和外部访问;
	char kind[60];
	char color[60];
	void print() {
		cout << "kind:" << kind << "	color:" << color << endl;
	}
	int get_size()//返回private下的变量值
	{
		return size = 1;
	}
	void set_size(int size)//给private下的变量值赋值
	{
		this->size = size;
	}
	void set_year()//给protected下的变量值赋值
	{
		cout << "请输入年龄:" << endl;
		cin >> year;
	}
	int get_year()//返回private下的变量值
	{
		return year;
	}
private:
	int size;
protected://保护控制权限,在类的继承中跟private有区别,但单个类中,跟private一样。
	int year;
};
int main(int argc, char** argv) {
	cout << "---------------class成员------------------" << endl;
	animal dog;
	strcpy_s(dog.color, "pig_dog");
	strcpy_s(dog.kind, "yellow");
	dog.print();
	cout << "---------------private成员----------------" << endl;
	// cout<< dog.size << endl;//会报如下错误。
	dog.set_size(1);
	cout << "size:" << dog.get_size() << endl;//但是可以通过在public下定义公有函数返回private变量值cout << "---------------protected成员----------------" << endl;
	dog.set_year();
	cout << "year:" << dog.get_year() << endl;
}

错误:没有size变量。
在这里插入图片描述
结果:
在这里插入图片描述

三.面向对象和面向过程

例子:注意看使用方法。

#include <iostream>
#include "total.h"
using namespace std;

class girlfriend {
public:
	char name[60];
	//面向对象
	void eat(string kind) {
		cout << "面向对象,女朋友:" << name << ",是" << kind << endl;
	}
};

//面向过程
void eat(class girlfriend& gf, string kind) {
	cout << "面向过程,女朋友:" << gf.name << ",是" << kind << endl;
}

int main(int argc, char** argv) {
	girlfriend gf;
	strcpy_s(gf.name, "C++");
	//面向对象
	gf.eat("pig");
	//面向过程
	eat(gf, "dog");
}

结果:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值