黑马程序员C++案例作业

104 类和对象-封装-案例1-立方体类

main.cpp

#include "Cube.h"
//#include <iostream>

void main() {
	Cube c1(10, 10, 10);
	Cube c2(11, 10, 10);

	c1.isSameClass(c2);
	isSame(c1, c2);

	system("pause");
}

Cube.h

#pragma once
#include <iostream>

class Cube {
	//方法
public:
	Cube(int l, int w, int h) : _l(l), _w(w), _h(h) {

	}
	int getL() {
		return this->_l;
	}
	int getW() {
		return this->_w;
	}
	int getH() {
		return _h;
	}
	void isSameClass(Cube& c) {
		if (this->_h == c._h && this->_w == c._w && this->_l == c._l) {
			std::cout << "类内方法表示两个立方体是相等的" << std::endl;
		}
		else {
			std::cout << "类内方法表示两个立方体是不相等的" << std::endl;
		}
	}
	//属性
private:
	int _l;
	int _w;
	int _h;
};

void isSame(Cube& c1, Cube& c2);

Cube.cpp

#include "Cube.h"


void isSame(Cube& c1, Cube& c2){
	if (c1.getH() == c2.getH() && c1.getW() == c2.getW() && c1.getL() == c2.getL()) {
		std::cout << "全局方法表示两个立方体是相等的" << std::endl;
	}
	else {
		std::cout << "全局方法表示两个立方体是不相等的" << std::endl;
	}
}
105 类和对象-封装-案例2-点和圆的关系

main.cpp

#include "Circle.h"
#include "Point.h"

void main() {
	Point yuanxin(0, 0);
	Circle c(5, yuanxin);
	Point p(3, 4);
	c.isInCircle(p);
	system("pause");
}

Point.h

#pragma once
//#include "Circle.h"

class Point {
friend class Circle;
public:
	Point() {

	}
	Point(int x, int y) :_x(x), _y(y) {

	}

private:
	int _x;
	int _y;
};

Circle.h

#pragma once
#include "Point.h"
#include <iostream>
class Circle {
public:
	Circle(int radius, Point p) :_radius(radius), _p(p) {}
	void isInCircle(Point& p);
private:
	int _radius;
	Point _p;
};

Circle.cpp

#include "Circle.h"
#include "Point.h"
#include <math.h>

void Circle::isInCircle(Point& p) {
	
	int ret = sqrt(pow(this->_p._x - p._x, 2) + pow(this->_p._y - p._y, 2));
	std::cout << ret<<std::endl;
	if (ret == this->_radius) {
		std::cout << "圆上" << std::endl;
	}
	else if (ret < this->_radius) {
		std::cout << "圆内" << std::endl;
	}
	else if (ret > this->_radius) {
		std::cout << "圆外" << std::endl;
	}
}
137 类和对象-多态-案例1-计算器

main.cpp

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

void main() {
	//多态的使用条件 父类的指针或引用指向子类的指针
	AbstractCaculator* ac=new AddCaculator;
	ac->_a = 10;
	ac->_b = 10;
	cout << ac->getResult() << endl;

	AbstractCaculator* mc = new MulCaculator;
	mc->_a = 10;
	mc->_b = 10;
	cout << mc->getResult() << endl;

	system("pause");

}

Caculator.h

#pragma once
#include <string>
using namespace std;

class AbstractCaculator {
public:
	//构造函数不能是virtual
	//继承时,构造函数不会自动继承,如果不为子类写构造函数,则子类只有默认空构造函数
	//						   如果为子类写构造函数,则在调用父类构造函数后,调用子类构造函数
	//当父类写了有参构造函数 子类就不再提供默认构造函数,且写默认构造函数会出错,所以要再为父类提供默认构造函数
	AbstractCaculator(int a, int b) : _a(a), _b(b) {

	}
	AbstractCaculator(){}
	virtual int getResult() {
		return 1;
	}

	int _a;
	int _b;
};

class AddCaculator :public AbstractCaculator {
public:
	int getResult() {
		return _a + _b;
	}
};

class MulCaculator :public AbstractCaculator {
public:
	int getResult() {
		return _a * _b;
	}
};
139 类和对象-多态-案例2-制作饮品

main.cpp

#include "MakeDrink.h"

void test(MakeDrink* ms) {
	ms->MakeSomething();
	
}

void main() {
	test(new MakeTea);
	std::cout << "___________________" << std::endl;
	test(new MakeCoffee);
	system("pause");
}

MakeDrink.h

#pragma once
#include <iostream>
class MakeDrink {
public:
	virtual void boil() = 0;
	virtual void putSomething() = 0;

	void MakeSomething() {
		boil();
		putSomething();
	}
};
class MakeTea:public MakeDrink {
	void boil() {
		std::cout << "煮农夫山泉" << std::endl;
	}
	void putSomething() {
		std::cout << "放茶叶" << std::endl;
	}
};
class MakeCoffee:public MakeDrink {
	void boil() {
		std::cout << "煮水" << std::endl;
	}
	void putSomething() {
		std::cout << "放咖啡豆" << std::endl;
	}
};

猜你喜欢

转载自blog.csdn.net/qq_39006214/article/details/117481134