第六章 面向对象面向对象程序设计 简单

/*
// 4 声明一个类
#include <iostream>
using namespace std;
class Human
{
public:
	void GetStature();
	void getWeight();
private:
	int stature;
	int weigth;
};
int main()
{
    return 0;
}

*/


/*
//6 定义一个对像
#include <iostream>
using namespace std;
class human
{
public:
	void getStartue(){cout<<startue; }
	void setStartue(int x){startue = x;}
	void getWeight();
	void setWeight(int y);
private:
	int startue;
	int weight;
};
void human::getWeight()
{
    cout<<weight;
};
void human::setWeight(int y)
{
    weight = y;
};
int main()
{
	human man;
	man.setStartue(18);
	man.setWeight(112);
	cout<<"年龄:";
	man.getStartue();
	cout<<", 体重:";
	man.getWeight();
	cout<<endl;

    return 0;
}
*/


//8公有与私有
/*#include <iostream>
using namespace std;
class Human
{
public:
	void setWeigth(int w)
	{
		if(w > 0 && w < 100){
		    weigth = w;
		}else{
		    cout<<"值只能大于1并且小于100"<<endl;
		}
	}
	int getWeigth(){ return weigth;}
private:
	int weigth;
};
int main()
{
	Human human;
	human.setWeigth(222);
	cout<<"体重为:"<<human.getWeigth()<<endl;
	
	Human tow;
	tow.setWeigth(33);
	cout<<"体重为:"<<tow.getWeigth()<<endl;
    return 0;
}
*/

/*
//10 友联函数
#include <iostream>
using namespace std;
class Human
{
public:
	inline void func(int);
	int get(){return x;}
private:
	int x;
};
void Human::func(int s){ x=s;};
int main()
{
	Human human;
	human.func(22);
	cout<<human.get()<<endl;

    return 0;
}
*/

#include <iostream>
using namespace std;
class Human
{
public:
	void func(int x, int s){ i=x, y=s;};
	void print(){
		cout<<"i:"<<i<<", y:"<<y<<endl;
		cout<<"两个数相X为:"<<i*y<<endl;
	};
private:
	int i;
	int y;
};

/*#include "11_human.h";
int main()
{
	Human human;
	human.func(22,33);
	human.print();
    return 0;
}*/


/*
//12 const 成员函数
//const函数只读操作符
#include <iostream>
using namespace std;
class Human
{
public:
	void func(int x, int s){ i=x, y=s;};
	void print()const{
		cout<<"i:"<<i<<", y:"<<y<<endl;
		cout<<"两个数相X为:"<<i*y<<endl;
	};
private:
	int i;
	int y;
};
int main()
{
   Human human;
   human.func(22,22);
   human.print();
   return 0;
}
*/


/*
// 13构造函数
#include <iostream>
using namespace std;
class Rectangel
{
public:
	Rectangel(){};
	Rectangel(int l, int w){ lenght=l, width=w;};
	int area(){ return lenght * width;}
private:
	int lenght;
	int width;
};
int main()
{
	Rectangel rectangel(2,3);
	cout<<rectangel.area()<<endl;

	Rectangel xx;
	cout<<xx.area()<<endl;

    return 0;

}
*/

//15 析构函数
#include <iostream>
using namespace std;
class A
{
public:
	A(){cout<<"构造函数执行完毕"<<endl;}
	~A(){cout<<"析构函数执行完毕"<<endl;}
};
int main()
{
   A a;


   //析构对像数组
   cout<<"析构对像数组"<<endl;
   A b[2];

   return 0;
}

  

原文地址:https://www.cnblogs.com/xiangxiaodong/p/2552901.html