C++基础2(1):类与对象

1. 认识类与对象

  • 什么是类(class)?
    类(class)是类型(type),是用户自定义的类型。为什么不叫它type,因为借用Simula语言中的class关键字。
  • 为什么要有类?
    基于便利性的考虑,现实世界中物(object)通常被分为几种独立的分类。
  • 基本概念
概念比喻
对象/实例楼房
实例化建造
建筑图纸
  • 面向对象四大特征
特征说明类比
抽象抽出具体事物的普遍性的本质分门别类:鸟类、哺乳类、鱼类
封装把数据与处理(函数)包在一起通信录(增加、删除)
继承数据与处理(函数)的传承财富与绝技、混血儿(肤色/头发、 两种语言
多态同一个事物(函数)的多种形态手机键盘数字与字母、 电脑键盘功能键

2. 类的定义与对象创建

2.1 类的定义:与struct相似(C++)

   class 类名{
        成员变量成员函数声明
    };
    

class定义最后的;一定不要忘记。

  • 构成
构成作用
数据成员(data member)/成员变量/属性对象内部数据和状态,只能在类定义中声明,可以在成员函数中直接调用。
成员函数/方法对象相关的操作,可以在类内实现或类外实现。
  • 实例:复数Complex
#include <iostream>
using namespace std;
class Complex{
private:		// 成员变量一般是私有的,只能通过成员函数访问
	int real;
	int imag;
public:			// 成员函数一般是公有的,可以提供给对象对外调用
	void Init(int r, int i){
		real = r;
		imag = i;
	}
	void Init(int r){
		Init(r,0);	// 调用上面的Init函数
	}
	int GetReal(){
		return real;
	}
	int GetImag(){
		return imag;
	}	
	void SetReal(int r){
		real = r;
	}
	void SetImag(int i){
		imag = i;
	}
	void Print(){
		if(0!=real)
			cout << real;
		if(0!=real && 0<imag){
			cout << "+";
		}
		if(0!=imag)
			cout << imag << "i";
		cout << endl;
	}
	Complex Plus(Complex c){
		Complex res;
		res.real = real + c.real;
		res.imag = imag + c.imag;
		return res;
	}
	Complex Divide(Complex c){
		Complex res;
		res.real = real - c.real;
		res.imag = imag - c.imag;
		return res;
	}
	Complex Multiple(Complex c){
		Complex res;
		res.real = real*c.real - imag*c.imag;
		res.imag = real*c.imag + imag*c.real;
		return res;
	}
};

int main(){
	Complex c;
	c.Init(1,2);
	c.Print();
	Complex c2;
	c2.Init(0,3);
	c2.Print();
	Complex c3;
	c3.Init(1);
	c3.Print();
	Complex c4;
	c4.Init(-1,-1);
	c4.Print();
	Complex sum = c3.Plus(c2);
	sum.Print();
	Complex Divide = c3.Divide(c2);
	Divide.Print();
	Complex Multiple = c3.Divide(c4);
	Multiple.Print();
}
  • 作用域运算符:: – 函数归属
  • 访问限定符
限定符作用
private[默认]私有
public公开
protected保护
实践中,成员变量多数情况使用private或者protected,成员函数多数情况使用public。通常,通过成员函数改变对象的成员变量。

  • 类定义与类实现分离
  • 头文件 - 声明
    方式:#pragma once或者#ifnde…#endif
    作用:防止头文件二次编译
  • 源文件 - 实现
    引用头文件:#include <>(标准库函数)/#include “”(自定义/第三方函数)
  • vector与string (在之后做详细学习)
  • class与struct区别
  • struct.cpp
 #include <iostream>
using namespace std;
struct Student{
        char name[20];
        bool sex;
        int age;
};

int main(){
        struct Student s = {"John",false,30}; //在C语言中此处struct关键字不可以省略,在C++中可以。

        cout << s.name << endl;
        cout << s.sex << endl;
        cout << s.age << endl;
}
  • class.cpp
#include <iostream>
using namespace std;
class Student{
public:
		// 成员变量
        char name[20];
        bool sex;
        int age;
        // 成员函数
        void Print();
};

void Student::Print(){
        cout << "name:" << name << endl;
        cout << "age:" << age << endl;
        cout << "sex:" << sex << endl;
}

int main(){
        Student s = {"John",false,30};

        cout << s.name << endl;
        cout << s.sex << endl;
        cout << s.age << endl;
		// 调用成员函数
        s.Print();
}
  • struct_class.cpp
 #include <iostream>
    
    using std::cout;
    using std::endl;
    
    struct SPos {
        int x,y,z;
    };
    class CPos {
        int x,y,z;
    };
    int main(){
    #ifdef STRUCT
        SPos spos = {1,1,1};
        cout << "(" << spos.x << "," << spos.y << "," << spos.z << ")" << endl;
    #else
        CPos cpos = {1,1,1};
        cout << "(" << cpos.x << "," << cpos.y << "," << cpos.z << ")" << endl;
    #endif
    }
    

C++class与struct区别是:

  1. 默认的访问控制不同
    struct是public,class是private
  2. struct可以使用花括号内的初始值列表{…}初始化,class不可以(C++98不可以,C++11可以)。
  3. class 中可以定义函数,struct 不可以。
注意:
C++的struct可以有成员函数,而C不可以。
C++的struct可以使用访问控制关键字(public private protected),而C不可以。

SPos spos; // C++
struct SPos spos; // C/C++

成员变量默认初始化为随机值(主要影响指针)。

2.2 对象创建/实例化

直接创建 – 类作为类型定义变量 – 栈上创建

  • 例如:

基本类型

  int main(){
        int a = 10;
        int b(10);
        cout << a << " " << b << endl;
    }

基本类型的初始化新增语法:

int a(0);// 等价 int a = 0;
const float b(1.0);// 等价 const float b = 1.0;
  • 类类型
 // 定义类
    class Demo{};
    // 创建对象
    int main(){
        Demo d; // 变量(命名对象)
        Demo(); // 匿名对象
    }
  • 基本语法
    类名 对象名; // 调用默认构造函数
    类名(); // 创建匿名对象

动态创建 – 堆上创建

  • 例如

基本类型

  int* p = new int;
    delete p;
    p = NULL;

类类型

  // 定义类
    class Demo{};
    // 创建对象
    int main(){
        Demo* d = new Demo;
        delete d;
        d = NULL;
    }
    

基本语法

类名* 对象指针 = new 类名;// 调用默认构造函数
delete 对象指针;

对象指针new可以为对象设置初始值,例如下面代码

int* p = new int(100);
cout << *p << endl;

动态创建数组 – 堆上创建

  • 例如

基本类型

 int* pa = new int[10];
    delete pa;// 只释放p[0],数组的第一个元素
    delete [] pa;// 释放全部数组

类类型

 // 定义类
    class Demo{};
    // 创建对象
    int main(){
        Demo* d = new Demo[10];
        delete [] d;
        d = NULL;
    }

对象数组指针new不可以为对象设置初始值

int* pa = new int[10](100); // error: array 'new' cannot have initialization arguments 

注意:C++除了特殊情况,很少直接使用malloc()/free()申请释放内存,取而代之的是new/delete。
空结构体与空类的大小(sizeof)为1,主要在于初始化/实例化时,编译器给变量/对象分配内存(地址),内存最小单位为1个字节。
通常,sizeof(类型) == sizeof(变量)。

3. this指针

  • 作用域:类内部
  • 特点
    • 类的一个自动生成、自动隐藏的私有成员
    • 每个对象仅有一个this指针
    • 当一个对象被创建时,this指针就存放指向对象数据的首地址
    • 不是对象本身的一部分,不会影响sizeof(对象)的结果
如果成员函数形参与成员变量同名,使用this->做为前缀区分。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值