CPP入门第二天

一、面向对象

.h文件中不实现操作,实现操作均由.cpp文件中。

People.cpp

#include "People.h"

People::People() {}
People::People(int age ,int sex) {
	this->age = age;
	this->sex = sex;
}

int People::getAge() {
	return this->age;
}

char People::getName() {
	return this->name;
}

int People::getSex() {
	return this->sex;
}

 void People::sayHello() {
	 printf("Hello \n");
}

 void People::sayBaye() {
	 printf("Baye ");
 }

People.h

#include <stdio.h>
#include <iostream>

class People
{
private:
	int age;
	char name;
	int sex;

public :
	People();
	People(int age,int sex);
	void setAge(int age);
	void setName(char name);
	void setSex(int sex);
	int getAge();
	char getName();
	int getSex();
	void sayHello();
	virtual void sayBaye() = 0;
};
#include <iostream>
#include "People.h"
int main()
{
	People *p= new People();
	p->sayHello();
	p->sayBaye();
	return 0;
}

二、命名空间

命名空间主要解决 类名函数名相同。
创建命名空间时需要.cpp于.h文件都命名
创建命名空间 namespace 名称
使用命名空间 using namespace 名称
People.cpp

#include "People.h"
// namespace 名称
namespace hongzhe {

	People::People() {}
	People::People(int age ,int sex) {
		this->age = age;
		this->sex = sex;
	}

	int People::getAge() {
		return this->age;
	}

	std::string People::getName() {
		return this->name;
	}

	int People::getSex() {
		return this->sex;
	}

	 void People::sayHello() {
		 printf("Hello \n");
	}
}

#include <stdio.h>
#include <iostream>
namespace hongzhe {

	class People
	{
	private:
		int age;
		std::string name;
		int sex;

	public:
		People();
		People(int age, int sex);
		void setAge(int age);
		void setName(std::string name);
		void setSex(int sex);
		int getAge();
		std::string getName();
		int getSex();
		void sayHello();
	};

}

#include <iostream>
#include "People.h"

using namespace hongzhe;
int main()
{
	People *p = new People();
	p->sayHello();
	return 0;
}

三、继承

派生类可以访问基类中所有的非私有成员

#include "Man.h"

Man::Man() {}
Man::Man(int age, int sex) {
	this->age = age;
	this->sex = sex;
}

int Man::getAge() {
	return this->age;
}
std::string Man::getHair() {
	return this->hair;
}

std::string Man::getName() {
	return this->name;
}
int Man::getSex() {
	return this->sex;
}

void Man::sayBaye() {
	printf("Man˵Baye");
}
void Man::sayHello() {
	printf("Man˵Hello");
}
void Man::sayHello(std::string message) {
	std::cout << message << "˵ Hello\n";
}
#pragma once
#include "People.h"

class Man : public People
{
private:
	int age;
	std::string name;
	int sex;
	std::string hair;

public:
	Man();
	Man(int age,  int sex);
	int getAge();
	std::string getName();
	int getSex();
	std::string getHair();
	virtual void sayBaye();
	virtual void sayHello();
	void sayHello(std::string message);
};
#include <iostream>
#include "Man.h"

int main()
{
	People *p = new Man();
	p->sayHello();
	return 0;
}

一个子类可以继承多个父类,他继承了多个父类的特性

#include "Animal.h"
#include "People.h"
class WoMan:public Animal,public People
{
public:
	WoMan();
	~WoMan();
	void information();

	virtual void sayBaye();
};

#include "WoMan.h"

WoMan::WoMan() {
	information();
	sayBaye();
}

void WoMan::information() {
std::cout << "Age = " << getAge() << "\n Sex = " << getSex() << "\n Category = " << getCategory();
}

void WoMan::sayBaye() {
	std::cout << "sayBaye = ";
}
WoMan::~WoMan() {
	std::cout << "析构函数 ";
}
#include <iostream>
#include "WoMan.h"
//#include "People.h"

int main()
{

	WoMan woMan;
	woMan.age=20;
	woMan.sex= 1;
	woMan.name = "这是个女性";
	woMan.category="人族";
	woMan.information();
	return 0;
}

打印
在这里插入图片描述

四、析构函数

析构函数是将方法执行完成后执行
WoMan.h

~WoMan();

WoMan.cpp

WoMan::~WoMan()

五、虚函数、纯虚函数

虚函数:是被 __virtual __ 关键字修饰的函数

virtual void sayHello()

纯虚函数:基类不需要实现虚函数,它的实现让该基类的派生类。(是一种特殊的虚函数)

virtual void sayBaye() = 0;

目的:

  1. 为了安全,因为避免任何需要明确但是因为不小心而导致的未知的结果,提醒子类去做应做的实现。
  2. 为了提升编码效率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值