面向对象——类与对象

文章目录

类与对象

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;
} 
相关推荐
Antonio9154 分钟前
【CMake】使用CMake在Visual Studio内构建多文件夹工程
开发语言·c++·visual studio
LyaJpunov17 分钟前
C++中move和forword的区别
开发语言·c++
程序猿练习生22 分钟前
C++速通LeetCode中等第9题-合并区间
开发语言·c++·leetcode
z千鑫30 分钟前
【人工智能】如何利用AI轻松将java,c++等代码转换为Python语言?程序员必读
java·c++·人工智能·gpt·agent·ai编程·ai工具
一名路过的小码农32 分钟前
C/C++动态库函数导出 windows
c语言·开发语言·c++
Ddddddd_1581 小时前
C++ | Leetcode C++题解之第416题分割等和子集
c++·leetcode·题解
编程版小新2 小时前
C++初阶:STL详解(四)——vector迭代器失效问题
开发语言·c++·迭代器·vector·迭代器失效
AlexMercer10123 小时前
【C++】二、数据类型 (同C)
c语言·开发语言·数据结构·c++·笔记·算法
小灰灰爱代码3 小时前
C++——求3个数中最大的数(分别考虑整数、双精度数、长整数的情况),用函数模板来实现。
开发语言·c++·算法
BeyondESH5 小时前
Linux线程同步—竞态条件和互斥锁(C语言)
linux·服务器·c++