03 C++ 类的基础部分

面向对象

面向对象程序设计是以对象为中心,把数据和对数据的操作封装在一起

#include <iostream>
using namespace std;
struct student
{
    int number;
    char name[20];
    float score;
    void input_student()
    {cin>>number>>name>>score;}
    void output_student()
    {cout<<number<<name<<score<<endl;}
};
int main()
{
    struct student s = {0};
    s.input_student();
    s.output_student();
    return 0;   
}

是一个自定义的数据类型,将数据(属性)和操作数据的函数(方法)封装在一起

声明:

class class-name 
{
private:
    declaration;
    // ... more declarations may follow...
public:
    declaration;
    // ... more declarations may follow...
}; 

默认情况下类的成员是私有的,而结构体struct中的成员是公有的

为了使类的成员能够在类外面被访问,其成员必须定义为public

成员函数

在类内定义与普通函数定义类似

在类外定义(在类内要声明):

<返回值类型> <类名>::<函数名>(参数列表)
{......}
void Rectangle::setData(float w,float l)
{
    width=w;
    length=l;
}

私有函数成员

​ 专门用于内部处理的函数,它们在类的外部不能使用,这些函数为私有的

私有函数可以被同一个类中的其它函数调用

定义对象

定义对象称为类的实例化(模具—铸件)

Rectangle box;
box.setData(10.0, 12.5);
cout << Box.getWidth( );
Rectangle *boxPtr;
boxPtr = &box;
boxPtr->setData(15, 12);

类的多文件组织

  1. 类的声明存储在头文件里(类的声明文件)

  2. 成员函数定义存储在 .cpp 文件(类的实现文件)

  3. 用户程序(使用该类)通过 #include 包含头文件

// stu.h
#ifndef __STU__ // 使用 ifndef 避免重定义
#define __STU__
class student
{
public:
    int number;
    char name[20];
    float score;
    void input_student();    
    void output_student();
};
#endif
// stu.cpp
#include "student.h"
#include <iostream>
using namespace std;
void student::input_student()
{cin>>number>>name>>score;}
void student::output_student()
{cout<<number<<name<<score<<endl;}
// main.cpp
#include <iostream>
using namespace std;
#include "student.h"
#include "student.h"
int main()
{
    student s;
    student* p = &s;
    p->input_student();
    p->output_student();
    return 0;   
}

内联函数

在类定义内定义的函数默认为内联函数

class Rectangle{
private:
    float width,length,area;
public:
    void setData(float,float);
    void calculateArea(){area=width*length;} // 内联函数
    ...
    void Rectangle::setData(float w,float l);
};
inline void Rectangle::setData(float w,float l) // 内联函数
{
    width=w;
    length=l;
}

构造函数和析构函数

构造函数是一个函数成员,当定义类对象时,自动调用该函数对数据成员进行初始化

析构函数也是一个函数成员,当对象终止时将自动调用该函数进行“善后”处理

构造函数

  • 构造函数是与类同名的函数成员

  • 没有返回值类型,也不允许有void

  • 在对象创建时,由系统自动调用

  • 如果程序中未声明,则系统自动产生出一个缺省形式的构造函数

  • 允许为内联函数、重载函数、带缺省形参值的函数

class student
{
private:
    ...
public:
    student(int n,const char *na,float s)
    {
        cout<<"in constructor"<<endl;
        number=n;
        name=new char[20];
        strcpy(name,na);
        score=s;
    }
    ...
};
int main()
{
    student s(1,"lisi",98); // 调用构造函数 stack
    student* p = &s; // 不会调用构造函数 只是让指针指向
    p->output_student();
    student& r = s; // 不会调用构造函数 只是取个别名(实质上是指针)
    r.output_student();
    student* q = new student(2,"wu",66); // 调用构造函数 heap
    q->output_student();
    delete q; // 调用析构函数
    return 0;   
}

带参构造函数和缺省构造函数

  • 如果类中没有定义构造函数,系统将提供一个无参构造函数(属缺省构造函数),该函数不实现任何功能
  • 如果类中定义有带参的构造函数,并且所有形参均具有缺省值,那么该构造函数也属于缺省的构造函数
student()
{
    cout<<"in constructor"<<endl;
    number=0;
    name=new char[20];
    strcpy(name,"null");
    score=0;
}
student(int n,const char *na="null",float s=0.0)
{
    cout<<"in constructor"<<endl;
    number=n;
    name=new char[20];
    strcpy(name,na);
    score=s;
}

析构函数

  • 析构函数也与类同名,前面加波浪号~

  • 当一个对象终止时会自动调用析构函数

~student()
{
    cout<<"in destructor"<<endl;
    delete[] name;
}
  • delete 对象时会调用析构函数
student* q = new student(2,"wu",66); // 调用构造函数 heap
delete q; // 调用析构函数
  • 一个类只有一个析构函数
  • 同构造函数一样,析构函数也没有返回值类型
  • 析构函数无参数

对象数组

  • 创建对象数组时,数组中每个元素(对象)都将调用构造函数
  • 如果没有为数组元素指定显式初始值,数组元素便使用缺省值初始化(调用缺省构造函数)
  • 当数组中每一个对象被删除时,都要调用一次析构函数
student s[10]={student(1),student(2,"wang","98")};
Student* students = new Student[3]
{
    Student(20, "Alice"),
    Student(21, "Bob"),
    Student(22, "Charlie")
};

this 指针

void print() // 编译器编译时自动在括号内加入 *this
{
    // cout<<name<<" "<<age<<endl;
    // same as:
    cout << this->name << " " << this->age << endl;
}
posted @ 2025-04-23 18:33  YamadaRyou  阅读(23)  评论(0)    收藏  举报