面向对象——类与对象

发布于:2024-04-18 ⋅ 阅读:(19) ⋅ 点赞:(0)

类与对象

#include<iostream>
#include<string>
using namespace std;
/*
类与对象 
*/
class Person{
	public:
		string name;// 固有属性,成员变量 
		int age;
	public:
		void eat(){ // 成员函数,成员方法 
			cout<<"eat()"<<endl;
		}
		void show(){
			cout<<"[name:"<<name<<", age:"<<age<<"]"<<endl;
		} 
};
int main(){
	Person p1;  // 实例化对象 
	p1.name = "AAA";
	p1.age = 11;
	
	p1.eat();
	p1.show();
	return 0;
} 

构造函数、析构函数

#include<iostream>
#include<string>
using namespace std;

/*
构造函数

类成员属性 
public属性的成员对外可见,对内可见。
private属性的成员对外不可见,对内可见。
protected属性的成员对外不可见,对内可见,且对派生类是可见的。
*/

class Person{
	public: // 公开,哪里都可以访问 
		string name;// 固有属性,成员变量 
		int age;
	public: // 公开,哪里都可以访问 
		Person(){// 无参构造 
			cout<<"构造函数:Person()"<<endl;
		} 
		Person(string _name,int _age){// 有参构造函数  
			name = _name;
			age = _age;
			cout<<"构造函数:Person(string _name,int _age)"<<endl;
		}
		Person(const Person& p){ // 复制构造函数 
			name = p.name;
			age = p.age;
			cout<<"构造函数:Person(const Person& p)"<<endl;
		}
		~Person(){ // 析构函数
		// 析构函数:无法重载,析构顺序与构造顺序相反 
			cout<<"~Person()"<<name<<endl; 
		}
		void show(){ // 成员函数,成员方法 
			cout<<"[name:"<<name<<", age:"<<age<<"]"<<endl;
		}
};
int main(){
	Person p1;  // 实例化对象,调用无参构造函数 
	p1.name = "AAA"; // error
	p1.age = 11;
	p1.show();

	Person p2("BBB", 12);// 实例化对象,调用有参构造函数 
	p2.show();
	
	Person p3(p1);
	p3.show(); 
	return 0;
} 

get/set方法

#include<iostream>
#include<string>
using namespace std;

/*
get/set方法 
*/
class Person{
	private: // 私有,仅类内可以访问 
		string name;// 固有属性,成员变量 
		int age;
	public: // 公开,哪里都可以访问 
		Person(){// 无参构造 
			cout<<"构造函数:Person()"<<endl;
		} 
		Person(string _name,int _age){// 有参构造函数  
			name = _name;
			age = _age;
			cout<<"构造函数:Person(string _name,int _age)"<<endl;
		}
		Person(const Person& p){ // 复制构造函数 
			name = p.name;
			age = p.age;
			cout<<"构造函数:Person(const Person& p)"<<endl;
		}
		~Person(){ // 析构函数
		// 析构函数:无法重载,析构顺序与构造顺序相反 
			cout<<"~Person()"<<name<<endl; 
		}
		// 提供get/set方法 
		void setName(string _name){ name = _name; } 
		string getName(){ return name; }
		void setAge(int _age){ age = _age; }
		int getAge(){ return age; }

		void show(){ // 成员函数,成员方法 
			cout<<"[name:"<<name<<", age:"<<age<<"]"<<endl;
		}
};
int main(){
	Person p1;  // 实例化对象,调用无参构造函数 
//	p1.name = "AAA"; // error
//	p1.age = 11;
	p1.setName("AAA");
	p1.setAge(11);
	p1.show();

	Person p2("BBB", 12);// 实例化对象,调用有参构造函数 
	p2.show();
	
	Person p3(p1);
	p3.setName("CCC");
	p3.show(); 
	return 0;
} 

函数:类内声明、类外定义

#include<iostream>
#include<string>
using namespace std;

/*
函数:类内声明、类外定义 
*/

class Person{
	private: // 私有,仅类内可以访问 
		string name;// 固有属性,成员变量 
		int age;
	public: // 公开,哪里都可以访问 
		Person(); // 无参构造函数的声明 
		Person(string _name,int _age);// 有参构造函数的声明  
		Person(const Person& p); // 复制构造函数的声明 
		~Person(); // 析构函数的声明 
		
		// 提供get/set方法 
		void setName(string _name){ name = _name; } 
		string getName(){ return name; }
		void setAge(int _age){ age = _age; }
		int getAge(){ return age; }
		void show(){ // 成员函数,成员方法 
			cout<<"[name:"<<name<<", age:"<<age<<"]"<<endl;
		}
};

// 构造函数的类外实现 
Person::Person(){// 无参构造 
	cout<<"构造函数:Person()"<<endl;
} 
Person::Person(string _name,int _age){// 有参构造函数  
	name = _name;
	age = _age;
	cout<<"构造函数:Person(string _name,int _age)"<<endl;
}
Person::Person(const Person& p){ // 复制构造函数 
	name = p.name;
	age = p.age;
	cout<<"构造函数:Person(const Person& p)"<<endl;
}
Person::~Person(){ // 析构函数
// 析构函数:无法重载,析构顺序与构造顺序相反 
	cout<<"析构函数:~Person()"<<name<<endl; 
}

int main(){
	Person p1; 
//	p1.name = "AAA"; // error
//	p1.age = 11;
	p1.setName("AAA");
	p1.setAge(11);
	p1.show();

	Person p2("BBB", 12);// 实例化对象,调用有参构造函数 
	p2.show();
	
	Person p3(p1);
	p3.setName("CCC");
	p3.show(); 
	return 0;
} 

static

#include<iostream>
#include<string>
using namespace std;

/*
内联成员函数,使用inline关键字将函数定义为内联函数。
对于成员函数来说,如果其定义是在类体中,即使没有使用inline关键字,该成员函数也被认为是内联成员函数。

static 关键字: 静态成员属于类 
对于静态成员来说,不仅可以通过对象访问,还可以直接使用类名访问:
----------------临时分割线 
静态数据成员可以是当前类的类型,而其他数据成员只能是当前类的指针或引用类型
类的静态成员函数只能访问类的静态数据成员,而不能访问普通的数据成员。
静态成员函数不能定义为const成员函数,即静态成员函数末尾不能使用const关键字。
*/

class Person{
	private: // 私有,仅类内可以访问 
		string name;// 固有属性,成员变量 
		int age;
	public: // 公开,哪里都可以访问 
		static int cnt; 
		Person(); // 无参构造函数的声明 
		Person(string _name,int _age);// 有参构造函数的声明  
		Person(const Person& p); // 复制构造函数的声明 
		~Person(); // 析构函数的声明 
		
		// 提供get/set方法 
		void setName(string _name){ name = _name; } 
		string getName(){ return name; }
		void setAge(int _age){ age = _age; }
		int getAge(){ return age; }

		void show(){ // 成员函数,成员方法 
			cout<<"[name:"<<name<<", age:"<<age<<"]"<<endl;
		}
};
int Person::cnt = 0; // 初始cnt 

// 构造函数的类外实现 
Person::Person(){// 无参构造 
	cnt ++;
	cout<<"构造函数:Person()"<<endl;
} 
Person::Person(string _name,int _age){// 有参构造函数  
	cnt ++;
	name = _name;
	age = _age;
	cout<<"构造函数:Person(string _name,int _age)"<<endl;
}
Person::Person(const Person& p){ // 复制构造函数 
	cnt ++;
	name = p.name;
	age = p.age;
	cout<<"构造函数:Person(const Person& p)"<<endl;
}
Person::~Person(){ // 析构函数
	cnt --; 
// 析构函数:无法重载,析构顺序与构造顺序相反 
	cout<<"析构函数:~Person()"<<name<<endl; 
	cout<<Person::cnt<<endl;
}
int main(){
//	cout<<cnt<<end; // error
	cout<<Person::cnt<<endl; // 0
	
	Person p1; // 实例化对象,调用无参构造函数 
//	p1.name = "AAA"; // error
//	p1.age = 11;
	p1.setName("AAA");
	p1.setAge(11);
	p1.show();
	cout<<Person::cnt<<endl; // 1

	Person p2("BBB", 12);// 实例化对象,调用有参构造函数 
	p2.show();
	cout<<Person::cnt<<endl; // 2
	
	Person p3(p1);
	p3.setName("CCC");
	p3.show(); 
	cout<<Person::cnt<<endl; // 3
	cout<<p3.cnt<<endl; // 3 
	return 0;
}