← Back to Index
Research & Engineering Archive

OOPC 2. Classes

By Jingnan Huang · September 27, 2025 · 2060 Words

OOPC2.Classes
#

Last Edit: 9/28/25

2.1 Structs and Classes
#

struct Student {
    string name;
    int ID;
};

这里只是定义结构体,不会分配内存

Create an instance
#

struct Student x, y;
x.name = "Kenya";
x.ID = 8012;
y.name = "Cindy";
y.ID = 2023;

Print struct data #

Name: Kenya
ID: 8012

2.1.1 What is a Class
#

2.1.2 Defining a class
#

class Student {
    string name;
    int ID;
    void print();
};

函数 print() 这里只 declaration),还没有 implementation

2.1.3 Access Control
#

访问权限 含义
private(私有) 只能在类的内部访问
public(公开) 可以在类的外部访问
class Student {
private:
    string name;
    int ID;
public:
    void print();
};

2.1.4 Implementing functions in a class
#

void Student::print() {
    cout << "Name: " << name << endl << "ID: " << ID << endl;
}

2.1.5 Creating instances of a class
#

Student x;
x.name = "Cindy"; // ❌ 错误:name 是 private 成员
x.print();

虽然是 private ,但 x 中的 nameID 会占据内存,但没赋初值,会是垃圾值(?

2.1.6 Getter and Setter Functions
#

#include <string>
using namespace std;

class Student {
 private:
  string name;
  int ID;

 public:
  void print();
  void setName(string name);
  string getName();
};
#include <iostream>
#include "Student.h"
using namespace std;

void Student::print() {
  cout << "Name: " << name << endl << "ID: " << ID << endl;
}

void Student::setName(string n) {
  name = n;
}

string Student::getName() {
  return name;
}
Student x;
x.setName("Cindy");
cout << x.getName() << endl;
x.print();  // ID 仍然未初始化
Cindy
Name: Cindy
ID: -86764(未定义的随机值

这里 ID 就单纯一个 Class 中的 data

2.2 Constructors
#

2.2.1 Default Constructor
#

// Student.h
#include <string>
using namespace std;

class Student {
 private:
  string name;
  int ID;

 public:
  void print();
  void setName(string name);
  string getName();
};
// Student.cpp
#include <iostream>
#include "Student.h"
using namespace std;

void Student::print() {
  cout << "Name: " << name << endl << "ID: " << ID << endl;
}

void Student::setName(string n) {
  name = n;
}

string Student::getName() {
  return name;
}
// main.cpp
#include <iostream>
#include "Student.h"
using namespace std;

int main(void) {
  Student x;

  x.setName("Cindy");
  cout << x.getName() << endl;

  x.print(); // ID is not initialized
  return 0;
}
Name: Cindy
ID: 32767  // or -12345, or any random garbage value
class Student {
private:
    string name;
    int ID;
public:
    Student();  // Constructor
    void print();
    void setName(string name);
    string getName();
};
Student::Student() {
  ID = 0;
  name = "no name";
}
Name: no name
ID: 0

2.2.2 Other Constructors
#

Student(int id);              // 第一个构造函数,只传 id
Student(int id, string n);    // 第二个构造函数,同时传 id 和 name
Student::Student(int id) {
    ID = id;
    name = "no name";
}

Student::Student(int id, string n) {
    ID = id;
    name = n;
}
Student x;                   // Default constructor is called
Student y(20207);            // 1st constructor is called
Student z(20207, "Osiris");  // 2nd constructor is called
Student x:
Name: no name
ID: 0

Student y:
Name: no name
ID: 20207

Student z:
Name: Osiris
ID: 20207

2.2.3. How is it possible to have multiple constructors with the same name but different numbers of parameters?
#

默认的 Constructor 什么都干不了

2.3 Lifecycle of an Object
#

class Student {
public:
    Student();        // Constructor
    ~Student();       // Destructor
};

//可以通过两个对 Constructor 和 Destructor 的定义来可视化 Lifecycle

Student::Student(){
	cont << "Constructor called" << endl;
}

Studeng::~Student(){
	cont << "Destructor called" << endl;
}
int main(void) {
    Student x;  // Define x, Constructor Called
    cout << "Inside main" << endl;

    if(true) {
        Student y;  // Define y, another Constructor called
        cout << "Inside if" << endl;
    }  // Leaving y's scope, Destructor called
    cout << "Outside if" << endl;
    return 0;  // Whole game end, Destructor called
}
Constructor called
Inside main
Constructor called
Inside if
Destructor called
Outside if
Destructor called