C++面向对象——类与对象

类与对象

#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;
} 

this指针

同一类的各个对象创建后,都在类中产生自己成员的副本。
对象在副本中与成员函数建立关系是通过C++为成员函数提供的一个称为this的指针来进行的。

当创建一个对象时,this指针就初始化指向该对象。
当某一对象调用一个成员函数时,this指针将作为一个变元自动传给该函数。所以,不同的对象调用同一个成员函数时,编译器根据this指针来确定应该引用哪一个对象的数据成员。

this指针是由C++编译器自动产生且较常用的一个隐含对象指针,它不能被显式声明。
this指针是一个局部量,局部于某个对象。
this指针是一个常量,它不能作为赋值、递增、递减等运算的目标对象。
只有非静态类成员函数才拥有this指针,并通过该指针来处理对象。

友元

  1. 将普通函数声明为友元函数
#include <iostream>
using namespace std;  
// 将普通函数声明为友元函数的程序示例。
class Date {
   private:       // 数据成员
    int year;     // 年
    int month;    // 月
    int day;      // 日
   public:        // 公有成员
    Date(int y, int m, int d) : year(y), month(m), day(d) {}  // 构造函数
    friend void Show(const Date& dt);  // 输出日期,声明为友元
    void display(){
	    cout << year << "年" << month << "月" << day << "日" << endl;
	}
};
void Show(const Date& dt) {  // 输出日期
    cout << dt.year << "年" << dt.month << "月" << dt.day << "日" << endl;
}
int main() {
    Date dt(2009, 6, 18);  // 定义日期对象dt;
    Show(dt);              // 输出日期2009年6月18日
    dt.display();          // 通过成员函数访问 
    return 0;
}
  1. 将类Spouse声明为类Person的友元类
#include <cstring>
#include <iostream>
using namespace std;
// 将类Spouse声明为类Person的友元类
class Person;          // 对类Person的提前引用声明
class Spouse {         // 声明夫妻类
   private:            // 数据成员
    Person* pHusband;  // 丈夫
    Person* pWife;     // 妻子
   public:             // 公有成员
    Spouse(const Person& hus, const Person& wf);  // 构造函数
    ~Spouse() { delete pHusband; delete pWife; }  // 析构函数
    void Show() const;  // 输出信息
};

class Person {
   private:             // 数据成员
    char name[18];      // 姓名
    int age;            // 年龄
    char sex[3];        // 性别
   public:              // 公有成员
    Person(char* nm, int ag, char* sx) : age(ag) {  // 构造函数
        strcpy(name, nm), strcpy(sex, sx);
    }
    void Show() const { cout << name << " " << age << "岁 " << sex << endl; }
    friend class Spouse;  // 声明类Spouse为类Person的友元类
};

Spouse::Spouse(const Person& hus, const Person& wf) {
    pHusband = new Person(hus);  // 为丈夫对象分配存储空间
    pWife = new Person(wf);      // 为妻子对象分配存储空间
}

void Spouse::Show() const {  // 输出信息
    cout << "丈夫:" << pHusband->name << " " << pHusband->age << "岁" << endl;
    cout << "妻子:" << pWife->name << " " << pWife->age << "岁" << endl;
}

int main() {
    Person huf("张强", 32, "男");  // 定义丈夫对象
    Person wf("吴珊", 28, "女");   // 定义妻子对象
    Spouse sp(huf, wf);            // 定义夫妻对象
    huf.Show();                    // 输出丈夫信息
    wf.Show();                     // 输出妻子信息
    sp.Show();                     // 输出夫妻信息
    return 0;                      // 返回值0, 返回操作系统
}
  1. 将另一个类的成员函数声明为一个类的友元函数
#include <cstring>
#include <iostream>
using namespace std;
// 将另一个类的成员函数声明为一个类的友元函数
class Person;          // 对类Person的提前引用声明
class Spouse {         // 声明夫妻类
   private:            // 数据成员
    Person* pHusband;  // 丈夫
    Person* pWife;     // 妻子
   public:             // 公有成员
    Spouse(const Person& hus, const Person& wf);  // 构造函数
    ~Spouse() {
        delete pHusband;
        delete pWife;  // 析构函数
    }
    void Show() const;  // 输出信息
};
class Person {
   private:             // 数据成员
    char name[18];      // 姓名
    int age;            // 年龄
    char sex[3];        // 性别
   public:              // 公有成员
    Person(char* nm, int ag, char* sx) : age(ag) {  // 构造函数
        strcpy(name, nm), strcpy(sex, sx);
    }
    void Show() const {  // 输出信息
        cout << name << " " << age << " " << sex << endl;
    }
    // 声明类Spouse的成员函数Show()为类Person的友元函数
    friend void Spouse::Show() const;
};
Spouse::Spouse(const Person& hus, const Person& wf) {
    pHusband = new Person(hus);  // 为丈夫对象分配存储空间
    pWife = new Person(wf);      // 为妻子对象分配存储空间
}
void Spouse::Show() const {  // 输出信息
    cout << "husband:"<<pHusband->name<<" "<<pHusband->age<<" "<<endl;
    cout << "wife:" << pWife->name << " " << pWife->age << " " << endl;
}
int main() {                       // 主函数main()
    Person huf("张强", 32, "男");  // 定义丈夫对象
    Person wf("吴珊", 28, "女");   // 定义妻子对象
    Spouse sp(huf, wf);            // 定义夫妻对象
    huf.Show();                    // 输出丈夫信息
    wf.Show();                     // 输出妻子信息
    sp.Show();                     // 输出夫妻信息
    return 0;                      // 返回值0, 返回操作系统
}

名字空间

#include <iostream>
using namespace std;

namespace A {
    string a;
    void show() {
        cout << "A::show()" << endl;
    }
}

int main() {
    A::show();
    using namespace A;
    show();
    return 0;
}
#include <iostream>
#include <string>

namespace A {
int a, b, c, d;

int gcd(int a, int b) {
    return b ? gcd(b, a % b) : a;
}
void show() {
    std::cout << "A::show()\n";
}
}  // namespace A

namespace B {
int a, b, c, d;
void show() {
    std::cout << "B::show()\n";
}
}  // namespace B

int main() {
    // a = 1;// error
    // cout << a << "\n";// error
    A::a = 1;
    std::cout << A::a << "\n";
    A::show();
    B::show();

    using namespace std;
    using namespace A;
    cout << a << "\n";
    show();  // 调用的是 A::show();

    using namespace B;
    // cout << a << "\n";// error
    // show(); // error
    cout << A::a << "\n";
    cout << B::a << "\n";
    cout << A::gcd(12, 24) << endl;
    return 0;
}
  • 嵌套名字空间
#include <iostream>
using namespace std;

namespace A {
    string a;
    void show() {
        cout << "A::show()" << endl;
    }
    namespace B {
        string b;
        void show() {
            cout << "A::B::show()" << endl;
        }
    }  // namespace B
}  // namespace A

int main() {
    A::show();
    A::B::show();

    using namespace A;
    show();
    B::show();
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值