面向对象——类与对象

文章目录

类与对象

cpp 复制代码
#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;
} 

构造函数、析构函数

cpp 复制代码
#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方法

cpp 复制代码
#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;
} 

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

cpp 复制代码
#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

cpp 复制代码
#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;
} 
相关推荐
KyollBM23 分钟前
【CF】Day75——CF (Div. 2) B (数学 + 贪心) + CF 882 (Div. 2) C (01Trie | 区间最大异或和)
c语言·c++·算法
feiyangqingyun35 分钟前
Qt/C++开发监控GB28181系统/取流协议/同时支持udp/tcp被动/tcp主动
c++·qt·udp·gb28181
CV点灯大师37 分钟前
C++算法训练营 Day10 栈与队列(1)
c++·redis·算法
成工小白2 小时前
【C++ 】智能指针:内存管理的 “自动导航仪”
开发语言·c++·智能指针
sc写算法2 小时前
基于nlohmann/json 实现 从C++对象转换成JSON数据格式
开发语言·c++·json
SunkingYang2 小时前
C++中如何遍历map?
c++·stl·map·遍历·方法
Andrew_Xzw2 小时前
数据结构与算法(快速基础C++版)
开发语言·数据结构·c++·python·深度学习·算法
库库的里昂2 小时前
【C++从练气到飞升】03---构造函数和析构函数
开发语言·c++
momo卡2 小时前
MinGW-w64的安装详细步骤(c_c++的编译器gcc、g++的windows版,win10、win11真实可用)
c语言·c++·windows
凤年徐5 小时前
【数据结构初阶】单链表
c语言·开发语言·数据结构·c++·经验分享·笔记·链表