C++重要知识点及示例代码

抽象的定义:抽象是对具体对象进行概括,抽出这一类对象的公共性质并加以描述的过程。

封装:将抽象得到的数据和功能相结合,形成一个有机的整体。

类的定义:类是对逻辑上相关的函数与数据的封装,它是对对象的抽象描述。

• 对象:对象是该类的某一特定实体,即类的变量。
• 构造函数:构造函数的作用是为对象分配存储空间,并将对象初始化为一个特定的状态。

示例代码:

#include<iostream>
#include<string>
using namespace std;
class Vehicle {
public:
	Vehicle(string a, string b) {
		com = a;
		owner = b;
	}
	void set() {
		com = "奥迪双钻";
		owner = "我的伙伴";
	}
	void print() {
		cout <<"制造商 " <<com << endl <<"所有者 "<< owner<<endl;
	}
	string getCom() {
		return com;
	}
	string getOwner() {
		return owner;
	}
private:
	string com;
	string owner;

};
class Truck:public Vehicle {
public:
	Truck(double c, int f, string a, string b) :Vehicle(a, b) {
		carr = c;
		this->f;
	}
	void setCarr(int a) {
		carr = a;
	}
	void setF(int b) {
		f = b;
	}
	void print1() {
		cout <<"载重量 "<< carr <<" 吨"<< endl<<"牵引力 " << f<<"磅";
	}
private:
	double carr;
	int f;
};
int main() {
	Vehicle v("null", "null");
	v.set();
	v.print();
	cout << "Truck t:"<<endl;
	Truck t(1,2,"null","null");
	t.set();
	t.setCarr(34);
	t.setF(12);
	t.print();
	t.print1();
}

运行结果:

制造商 奥迪双钻
所有者 我的伙伴
Truck t:
制造商 奥迪双钻
所有者 我的伙伴
载重量 34 吨
牵引力 12磅

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值