c面向对象(基础类)

主要是一个熟练过程,交两个作业

第一个作业,①熟悉成员函数,成员变量②私有成员,公共成员③构造函数,析构函数

注意:成员变量一般私有,用成员函数来访问成员变量
//OOP1.h
class Tricycle
{
public:
	Tricycle(int x);
	~Tricycle();
	void pedal();
	void brake();
	void setSpeed(int speed);
	int getSpeed();
private:
	int speed;
}; 
#include
#include"OPP1.h"

void Tricycle::setSpeed(int newspeed){
	if(newspeed >= 0)speed = newspeed;	
};
void Tricycle::brake(){
	setSpeed(speed - 1);	
	printf("speed now :%d\n",speed);
};
void Tricycle::pedal(){
	setSpeed(speed + 1);
	printf("speed now :%d\n",speed);
}
Tricycle::Tricycle(int initialspeed){
	setSpeed(initialspeed);
} 
Tricycle::~Tricycle(){
	printf("Good bye!");
}
int Tricycle::getSpeed() {
	return speed;
}

int main()
{
	Tricycle myTricycle = Tricycle(24);
	myTricycle.brake() ;
	myTricycle.brake() ;
	myTricycle.brake() ;
	return 0;
	
}

                                        

第二个作业:基础类和较复杂的类

 ①内联的两种方式②const成员函数③将其他类用作成员数据的类

class Point{
	private:
		int x;
		int y;
	public:
		int getX(){return x;}
		int getY(){return y;}
		void setX(int newx){x = newx;}
		void setY(int newy){y = newy;}
};

class Rectangle{
	private:
		int top;
		int bot;
		int left;
		int right;
		Point upleft;
		Point upright;
		Point boleft;
		Point boright;
	public:
		Rectangle(int newtop,int newbot, int newleft, int newright);
		~Rectangle();
		//边设置,点设置,读取点,读取边 
		void setTop(int newtop);
		void setBot(int newbot);
		void setLeft(int newleft);
		void setRight(int newright);
		
		void setUpleft(Point loc);
		void setUpright(Point loc);
		void setBoleft(Point loc);
		void setBoright(Point loc);
		
		int getTop() const {return top;}
		int getBot() const {return bot;}
		int getLeft() const {return left;}
		int getRight() const {return right;}
		
		Point getUpleft() const {return upleft;}
		Point getUpright() const {return upright;}
		Point getBoleft() const {return boleft;}
		Point getBoright() const {return boright;}
		int getArea();
		
};

#include
#include"OOP2.h"

Rectangle::Rectangle(int newleft,int newright, int newbot, int newtop) {
	top = newtop;
	bot = newbot;
	left = newleft;
	right = newright;
	
	upleft.setX(newleft);
	upleft.setY(newtop); 
	upright.setX(newright);
	upright.setY(newtop); 
	boleft.setX(newleft);
	boleft.setY(newbot); 
	boright.setX(newright);
	boright.setY(newbot); 
}

Rectangle::~Rectangle(){
	printf("Good bye");
}
void inline Rectangle::setTop(int a){
	top = a;
	upleft.setY(a);
	upright.setY(a); 
} 

void inline Rectangle::setBot(int a){
	bot = a;
	boleft.setY(a);
	boright.setY(a); 
} 

void inline Rectangle::setLeft(int a){
	left = a;
	upleft.setX(a);
	boleft.setX(a); 
} 

void inline Rectangle::setRight(int a){
	right = a;
	upright.setX(a);
	boright.setX(a); 
} 

void inline Rectangle::setUpleft(Point loc){
	setTop(loc.getY() );
	setLeft(loc.getX() );
	upleft = loc;
}
void inline Rectangle::setUpright(Point loc){
	setTop(loc.getY() );
	setRight(loc.getX() );
	upright = loc;
}
void inline Rectangle::setBoleft(Point loc){
	setBot(loc.getY() );
	setLeft(loc.getX() );
	boleft = loc;
}
void inline Rectangle::setBoright(Point loc){
	setBot(loc.getY() );
	setRight(loc.getX() );
	boright = loc;
}

int inline Rectangle::getArea() {
	return abs(left - right)*abs(top - bot);
}

int main(){
	Rectangle myRectangle = Rectangle(20,40,60,80);
	printf("The square of the area:%d\n", myRectangle.getArea() );
	
	Point x;
	x.setX(0);
	x.setY(0); 
	
	myRectangle.setBoleft(x);
	printf("The square of the area:%d\n", myRectangle.getArea() );
	
	return 0;
}

参考书籍----《c++入门经典》第八、九章

你可能感兴趣的:(学习,c++)