面向对象——类与对象

文章目录

类与对象

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;
} 
相关推荐
码匠许师傅19 小时前
【设计模式精讲】14.外观模式(Facade)
c++·设计模式·uml·外观模式
xxwxx__21 小时前
C++ list 深度解析:从使用原理到模拟实现
开发语言·c++·list
Chester_199921 小时前
CSP202303C.LDAP
开发语言·c++·蓝桥杯·stl
j7~1 天前
【C++】《unordered系列关联式容器以及哈希底层、哈希冲突、闭散列与开散列完整解析》
c++·哈希算法·开散列·闭散列·unordered_map·unordered_set·哈希底层结构
2402_882893861 天前
封装红黑树实现map和set —— 从源码到模拟实现
c++·set·map
神仙别闹1 天前
基于C++ MFC 实现的智慧公交系统
开发语言·c++·mfc
dear_bi_MyOnly1 天前
函数模块化:企业级项目高效之道
c++·后端·学习
码匠许师傅1 天前
【设计模式精讲】13.装饰器模式(Decorator)
c++·设计模式·uml·装饰器模式
OPEN-F1 天前
C++综合实战:面向对象图书管理系统
开发语言·c++
NeoGressAI外贸数字化1 天前
外贸独立站零询盘排查:从 Google Search Console 到 PageSpeed 的技术实操
开发语言·c++