温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

类和对象的实现

发布时间:2020-05-19 06:53:53 来源:网络 阅读:321 作者:拼命学 栏目:移动开发
#include<iostream>
using namespace std;
int main()
{
   int i1=1;
   double d1=3.21;
   cout<<"int->"<<i1<<endl<<"double->"<<d1<<endl;
   system("pause");
}
#include<iostream>
using namespace std;
class person
{
public:
	void Display()
	{
	    cout<<"name: "<<_name<<endl;
		cout<<"sex: "<<_sex<<endl;
		cout<<"age: "<<_age<<endl;
	}
	char* _name;
	char* _sex;
	int _age;
};
int main()
{
person p;
p._name="zhangsan";
p._sex="nan";
p._age=31;
p.Display ();
system("pause");
return 0;
}
实例化
#include<iostream>
using namespace std;
class person
{
public:
	void Display()
	{
	    cout<<"name: "<<_name<<endl;
		cout<<"sex: "<<_sex<<endl;
		cout<<"age: "<<_age<<endl;
	}
public:
	char* _name;
	char* _sex;
	int _age;
};
void test()
{
   person p;
   p._name ="hhh";
   p._sex ="男";
   p._age =20;
   p. Display();
}
int main()
{
test();
system("pause");
return 0;
}
日期类
构造函数
#include<iostream>
using namespace std;
class Date
{
public :
	Date(int year,int month,int day)
	{
	   cout<<"构造函数"<<endl;
	   _year=year;
	   _month=month;
	   _day=day;
	}
	void SetDate(int year,int month,int day)
	{
	   _year=year;
	   _month=month;
	   _day=day;
	}
	void Display()
	{
	  cout<<_year<<"-"<<_month<<"-"<<_day<<endl;
	}
private :
	int _year;
	int _month;
	int _day;
};
void test1()
{
  Date d1(2015,2,11);
  d1.Display ();
}
int main()
{
  test1();
  system("pause");
  return 0;
}
重载
#include<iostream>
using namespace std;
class Date
{
public :
	Date(int year,int month,int day)
	{
	   cout<<"构造函数"<<endl;
	   _year=year;
	   _month=month;
	   _day=day;
	}
	Date(int year,int month)
	{
	  _year=year;
	  _month=month;
	  _day=11;
	}
	Date(int year)
	{
	  _year=year;
	  _month=12;
	  _day=11;
	}
	void SetDate(int year,int month,int day)
	{
	   _year=year;
	   _month=month;
	   _day=day;
	}
	void Display()
	{
	  cout<<_year<<"-"<<_month<<"-"<<_day<<endl;
	}
private :
	int _year;
	int _month;
	int _day;
};
void test1()
{
  Date d1(2015,2,11);
  Date d2(2015,2);
  Date d3(2015);
  d1.Display ();
  d2.Display ();
  d3.Display ();
}
int main()
{
  test1();
  system("pause");
  return 0;
}
拷贝构造
#include<iostream>
using namespace std;
class Date
{
public :
	Date(int year,int month,int day);
	
	Date(const Date& d)
	{
		cout<<"拷贝构造函数"<<endl;
	   _year=d._year ;
	   _month=d._month ;
	   _day=d._day ;
	}
	void SetDate(int year,int month,int day)
	{
	   _year=year;
	   _month=month;
	   _day=day;
	}
	void Display()
	{
	  cout<<_year<<"-"<<_month<<"-"<<_day<<endl;
	}
private :
	int _year;
	int _month;
	int _day;
};
Date::Date(int year,int month,int day)
{
	   cout<<"构造函数"<<endl;
	   _year=year;
	   _month=month;
	   _day=day;
	}
void test1()
{
  Date d1(2015,2,11);
  d1.Display ();
  Date d2(d1);
  d2.Display ();
}
int main()
{
  test1();
  system("pause");
  return 0;
}
析构函数
#include<iostream>
using namespace std;
class Arr
{
public:
	Arr(size_t size)
	{
	    _prt=(int*)malloc(sizeof(int)*size);
	}
	~Arr()
	{
	   free(_prt);
	}
private:
	int* _prt;
};
int main()
{
   Arr(20);
   system("pause");
   return 0;
}
复数类
#include<iostream>
using namespace std;
class Complex
{
public :
	Complex(double real=0,double p_w_picpath=0)
	{
	    cout<<"Complex (double real=0,double p_w_picpath=0)"<<endl;
		_real=real;
		_p_w_picpath=p_w_picpath;
	}
	
	~Complex()
	{
		cout<<"~Complex()"<<endl;
	}
	
	Complex(const Complex& d)
	{
	    cout<<"Complex(comst Complex& d)"<<endl;
		_real=d._real ;
		_p_w_picpath=d._p_w_picpath ;
	}
	
	Complex& operator=(const Complex& d)
	{
	      cout<<"operator=(const Complex& d)"<<endl;
		  if(this!=&d)
		  {
		      this->_real=d._real;
			  this->_p_w_picpath=d._p_w_picpath;
		  }
		  return *this;
	}
	void Display()
	{
	        cout<<"Real:"<<_real<<"--Image:"<<_p_w_picpath<<endl;
	}
private:
	 double _real;
	 double _p_w_picpath;
};
void test()
{
   Complex d1(1.1,3.3);
   d1.Display ();
}
int main()
{
	test();
	system("pause");
   return 0;
}


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI