本文转载:可以点击这里
1.C++类和对象
#include<iostream>
using namespace std;
class Car{
public:
string brand; // 在类中定义的变量称为属性
string model;
int year;
};
int main(){
// Create an object of Car
Car carObj1; // 在类中定义的对象称为实例,或者称为对象,此时才会分配内存
carObj1.brand = "BMW";
carObj1.model = "X5";
carObj1.year = 1999;
// Create another object of Car
Car carObj2;
carObj2.brand = "Ford";
carObj2.model = "Mustang";
carObj2.year = 1969;
// Print attribute values
cout << carObj1.brand << " " << carObj1.model << " " << carObj1.year << endl;
cout << carObj2.brand << " " << carObj2.model << " " << carObj2.year << endl;
return 0;
}
1.2 Class(类方法)
#include<iostream>
using namespace std;
class Car{
public:
int speed(int maxSpeed);
void myMethod(){ // 类中的函数叫做方法(Method)
cout<<"Hello World";
}
};
int Car::speed(int maxSpeed){
return maxSpeed;
}
int main(){
Car myObj; // 创建对象
cout<<myObj.speed(200)<<endl; // 调用对象的方法
myObj.myMethod();
return 0;
}
1.3 Class(类 构造函数)
类的构造函数是一种特殊的成员函数,它用于创建对象时初始化对象的数据成员。在C++中,构造函数的名称必须与类的名称相同,且没有返回类型(不应该有void)。构造函数可以带有参数(参数列表可以为空),也可以被重载。
#include<iostream>
using namespace std;
class Car{ // The class
public: // Access specifier,访问修饰符
string brand; // Attribute,属性
string model; // Attribute,属性
int year; // Attribute,属性
Car(string x, string y, int z); // Constructor declaration,构造函数声明
};
// Constructor definition outside the class,构造函数定义在类外
Car::Car(string x, string y, int z) {
brand = x;
model = y;
year = z;
};
int main(){
// Create Car objects and call the constructor with different values,创建Car对象并使用不同的值调用构造函数
Car carObj1("BMW", "X5", 1999); // 注意:创建时就会调用里面的Car(string x, string y, int z)
Car carObj2("Ford", "Mustang", 1969);
cout << carObj1.brand << " " << carObj1.model << " " << carObj1.year << endl;
cout << carObj2.brand << " " << carObj2.model << " " << carObj2.year << endl;
return 0;
}
1.4 Class (类 访问范围public private)
public允许访问外部,private不允许
#include<iostream>
using namespace std;
class MyClass{
int b; // private by default,默认是私有的
public: // public access specifier,公有访问说明符
int x; // public attribute,公有属性
int y;
private: // private access specifier,私有访问说明符
int a;
};
int main(){
MyClass myObj;
myObj.x = 25;
myObj.y = 50;
// myObj.b = 100;
// myobj.a = 200;
cout<<myObj.x<<endl; // allowed (public)
cout<<myObj.y<<endl; // allowed (public)
// cout<<myObj.b<<endl; // not allowed (private)
// cout<<myObj.a<<endl; // not allowed (private)
return 0;
}
1.5 Class (类 访问修改私有变量)
在C++中,getter和setter是用于访问和修改私有成员变量的方法。Getter用于获取私有成员变量的当前值,而setter用于设置私有成员变量的新值。它们提供了对私有成员变量进行控制的简便方式,同时也可确保对这些成员变量的访问和修改按照类的设计进行,以确保程序的正确性和可维护性。
#include<iostream>
using namespace std;
class Employee {
private:
int salary;
public:
// Setter,功能:设置salary的值,避免直接访问私有变量
void setSalary(int s) {
salary = s;
}
// Getter,功能:获取salary的值
int getSalary() {
return salary;
}
};
int main(){
Employee emp;
emp.setSalary(50000);
cout << emp.getSalary();
return 0;
}
1.6 Class(类 继承)
#include<iostream>
using namespace std;
// Base class,基类
class Vehicle {
public:
string brand = "Ford";
Vehicle() {
cout << "This is a Vehicle" << endl;
}
};
// Derived class,派生类/继承类
class Car: public Vehicle {
public:
string model = "Mustang";
};
int main() {
Car myCar;
cout << myCar.brand << " " << myCar.model << endl;
return 0;
}
1.7 Class(类 多继承)
#include<iostream>
using namespace std;
// Base class
class MyClass {
public:
void myFunction() {
cout << "Some content in parent class." ;
}
};
// Another base class
class MyOtherClass {
public:
void myOtherFunction() {
cout << "Some content in another class." ;
}
};
// Derived class,同时继承两个父类
class MyChildClass: public MyClass, public MyOtherClass {
};
int main() {
MyChildClass myObj;
myObj.myFunction();
myObj.myOtherFunction();
return 0;
}
1.7 Class(类 protected)
private 只有自己可以用,protected 可以自己和自己的子类用,例如下面的子类就可以用:
#include<iostream>
using namespace std;
// Base class
class Employee{
protected: // Protected access specifier,只有自己和子类可以访问
int salary;
};
// Derived class
class Programmer: public Employee{
public:
int bonus;
void setSalary(int s){
salary = s;
}
int getSalary(){
return salary;
}
};
int main() {
Programmer myObj;
myObj.setSalary(50000);
myObj.bonus = 15000;
cout << "Salary: " << myObj.getSalary() << "\n";
cout << "Bonus: " << myObj.bonus << "\n";
return 0;
}