<c++> Classes and Objects | Object Orientation | Access Specifiers | Class Declaration | Creating Classes

foreword

  • From here we officially start learning c++object-oriented programming in China. Before learning, we need to understand what object-oriented programming is and the difference from process-oriented programming.

process oriented programming

Q: What is process-oriented programming?

A: Process-oriented programming is to analyze the steps needed to solve the problem, and then use functions to implement these steps step by step, and call them one by one when using them.

Object-Oriented Programming

Q: What is object-oriented programming?

Object-oriented is to decompose the constitutive problem into various objects. The purpose of establishing an object is not to complete a step, but to describe the behavior of something in the entire problem-solving step.

For example: such as designing a system for playing football

Process-oriented : Focus on realizing the processes of passing, dribbling, and shooting in football. Express them one by one with functions.

Object-Oriented : Focus on implementing class objects and their relationships, balls, players and their relationships.

what is class

Q: What is a class?

A: A class is a relatively complex data type formed by abstracting the common attributes and behaviors of similar objects. This, like a structure, is to describe a relatively complex object.

What is the difference between a class and a structure

  • c++Classes are extensions C++to C语言structs in

  • Compared with a structure, a class can use functions as members, which are generally declared in the class, and the function body is implemented outside the class.

  • Both structures and classes can be accessed (object name. member name/object pointer -> member name), but the difference is that three access specifiers (used to set access permissions) can be used in the class.

three access specifiers

c++There are three access specifiers in the class, namely public, private, protectedlet's take a look at their specific functions.

  • public, publicthe members defined after the specifier are accessible throughout the program.
  • private, privatemembers defined after the specifier can be accessed by member functions of the class, but not by objects of the class.
  • Members defined protectedafter the specifier can be accessed by member functions of derived classes, but not by objects of the class.

how to create a class

class declaration

Before creating a class, let's learn how to declare a class. Declaring a class in c++is similar to declaring a struct.
Here is a piece of code that declares a structure:

struct Student {
    
    //声明了一个名为Student的结构体类型     
	int num;    
	char name[20];    
	char sex; 
};
Student stu; //定义了结构体变量stu

Here is a piece of code that declares a class:

class Student  {
    
    //以class开头
	int num;    
	char name[20];    
	char sex;//以上3行是数据成员    
	void display() {
    
    //这是成员函数          
	cout <<″num:<< num << endl;
	cout <<″name:<< name << endl;       
	cout <<″sex:<< sex << endl;       
	}    
}
Student stu; //定义了结构体变量stu

You can see that the method of declaring a class is similar to the method of declaring a structure type. The difference is that a class is a generalized data type, and the data in this data type can contain both data and functions that operate on the data.

create class

There are generally two ways to create a class:

  • Declarations and definitions are all placed in the class
  • Declaration and definition separation

To give the simplest example, we create a dog class

Declarations and definitions are all placed in the class

#include <iostream>
#include<string.h>
using namespace std;
class Dog {
    
    
    public:
    //成员函数
    void set(char n[20], int a) {
    
    //实现setPoint函数
        strcpy(name,n);
        age = a;
    }
    void print() {
    
    //实现printPoint函数
        cout << "name: " << name << endl;
        cout << "age: " << age << endl;
    }
    private:
    //成员变量
       char name[20];
       int age;
};
 
int main() {
    
    
    Dog dog; //用定义好的类创建一个对象 
    dog.set("旺财", 2); //设置狗狗姓名,年龄 
    dog.print(); //输出狗狗的信息
    return 0;
}

In this program, the declaration and definition are all placed in the class. If there are many member functions in the class, it will be a lot messy to read.

Declaration and definition separation

#include <iostream>
#include<string.h>
using namespace std;
class Dog {
    
    
    public:
    //成员函数
		void set(char n[20], int a);
		void print();
    private:
    //成员变量
        char name[20];
        int age;
};

void Dog::set(char n[20], int a) {
    
    //实现setPoint函数
    strcpy(name,n);
    age = a;
}

void Dog::print() {
    
    //实现printPoint函数
    cout<< "name: " << name << endl;
    cout<< "age: " << age << endl;
}

int main() {
    
    
    Dog dog; //用定义好的类创建一个对象 
    dog.set("旺财", 2); //设置狗狗姓名,年龄 
    dog.print(); //输出狗狗的信息
    return 0;
}

In this program, the declaration and definition are separated, that is, the member function only declares the function prototype inside the class, and defines the function outside the class, so that all member functions can be seen in the class, and the implementation process cannot be seen, which is streamlined in comparison a lot of.

Note : When creating a class separately from the declaration and definition, you need to use the scope resolution operator (::) to identify the class to which the function belongs.

Guess you like

Origin blog.csdn.net/Ceylan__/article/details/129133512