400 028 6601

建站动态

根据您的个性需求进行定制 先人一步 抢占小程序红利时代

C++笔记(四)类和结构体,类的继承,多继承,菱形继承-创新互联

前提类和结构体结构体
struct struct_name {data_type member1;
   data_type member2;
   // ...
};
struct Student {string name;
   int age;
   int id;
};

结构体变量可以通过使用结构体类型名和点运算符来访问结构体成员。
例如:

Student s1;
s1.name = "John Doe";
s1.age = 21;
s1.id = 6;
// C语言的结构体
struct Student{char* name;
    int age;
    float score;
};

void struct_demo() {// 定义一个student1
    Student s1;
    s1.name = "cqy";
    s1.age = 18;
    s1.score = 100;
    // Student 2 
    Student s2;
    s2.name = "cxk";
    s2.age = 200;
    std::cout<< "Student 1 name "<< s1.name<< " age: "<< s1.age<< std::endl;
    std::cout<< "Student 2 name "<< s2.name<< " age: "<< s2.age<< std::endl;
}
// C++ 类
class Student{public: 
    char* name;
    int age;
    float score;
};

void class_demo() {// 定义一个student1
    Student s1;
    s1.name = "cqy";
    s1.age = 18;
    s1.score = 100;
    // Student 2 
    Student s2;
    s2.name = "cxk";
    s2.age = 200;
    std::cout<< "Student 1 name "<< s1.name<< " age: "<< s1.age<< std::endl;
    std::cout<< "Student 2 name "<< s2.name<< " age: "<< s2.age<< std::endl;
}
// C++ 类
class Student{public: 
    // 两个激活函数
    int get_age() {return age_; }
    void setAge(int age) {age_ = age; }
    void say();
private: 
    int age_;
    float score_;
};

void Student::say() {std::cout<< "cqy age is: "<< age_<< std::endl;
}

void class_demo() {// 定义一个student1
    Student s1;  // 实例化一个类对象
    s1.setAge(20);
    std::cout<< "Student 1 Age: "<< s1.get_age()<< std::endl;
    s1.say();
}
Student 1 Age: 20
cqy age is: 20
// C++ 类
class Student{public: 
    // 两个激活函数
    int get_age() {return age_; }
    void setAge(int age) {age_ = age; }
    void say();
private: 
    int age_;
    float score_;
};
#include#include "class_demo.h"

void Student::say() {std::cout<< "cqy age is: "<< age_<< std::endl;
    }

int main() {// 定义一个student1
    Student s1;  // 实例化一个类对象
    s1.setAge(20);
    std::cout<< "Student 1 Age: "<< s1.get_age()<< std::endl;
    s1.say();
    return 0;
}
// #pragma once
#ifndef CLASS_FIRST_
#define CLASS_FIRST_

namespace Demo{// C++ 类
class Student{public: 
    // 两个激活函数
    int get_age() {return age_; }
    void setAge(int age) {age_ = age; }
    void say();
private: 
    int age_;
    float score_;
};
} // namespace class_demo 

#endif
#include#include "class_demo.h"

namespace Demo{void Student::say() {std::cout<< "cqy age is: "<< age_<< std::endl;
    }
}
int main() {// 定义一个student1
    Demo::Student s1;  // 实例化一个类对象
    s1.setAge(20);
    std::cout<< "Student 1 Age: "<< s1.get_age()<< std::endl;
    s1.say();
    return 0;
}
类成员的访问权限类的初始化
#includeclass Person {public:
    // constructor with two parameters
    Person(std::string name = "default", int age = 0) : name_(name), age_(age) {}
    // member variables
    std::string name_;
    int age_;
};

int main() {Person p1("asd", 10);
    Person p2; 
    std::cout<< "P1 name is: "<< p1.name_<< std::endl;
    std::cout<< "P2 name is: "<< p2.name_<< std::endl;
    return 0;
}
// #pragma once
#ifndef CLASS_FIRST_
#define CLASS_FIRST_

namespace Demo{class Student{public: 
    // 保留构造函数
    Student() = default;
    // 构造函数的声明
    Student(const char* name  , int age, float score);

public: 
    // 两个激活函数
    int get_age() {return age_; }
    void setAge(int age) {age_ = age; }
    void say();
    void setName(char* name) {name_ = name;} 
    
private: 
    // 在定义的时候给成员变量赋予默认值
    const char* name_ = nullptr;
    int age_ = 0;
    float score_ = 0;
};
} // namespace class_demo 


#endif
#include#include "class_demo.h"

namespace Demo{// 类的初始化 
Student::Student(const char* name, int age, float score): name_(name), age_(age),score_(score) {std::cout<< "==============start Construct==============="<< std::endl;
        // 与上面同等写法
        // name_ = name;
        // age_ = age;
        // score_ = score;
        std::cout<< "================end Construct==============="<< std::endl;
    } 

void Student::say() {std::cout<< "cqy age is: "<< age_<< std::endl;
    }
}
int main() {// 定义一个student1
    Demo::Student s1("cqy", 20, 100);  // 实例化一个类对象, 这里调用我们定义的构造函数
    Demo::Student s2;  // 这里调用默认构造函数
    s1.setAge(20);
    std::cout<< "Student 1 Age: "<< s1.get_age()<< std::endl;
    s1.say();
    return 0;
}
看一个C++11的例子
#includeclass Shape {public:
  // 保留默认构造函数
  Shape() = default;
  // 自己定义一个构造函数初始化, C++ 11语法
  Shape(int width, int height): width_(width), height_(height) {}
  //  当函数是 const 时,表示它不会改变对象的状态。这是一种好的编程习惯,
  // 因为它可以防止意外修改对象的状态
  int GetWidth() const {return width_;}
  int GetHeight() const {return height_;}

  // 改变对象状态的函数
  // 改变对象状态,就是通过函数来修改对象的成员变量的值
  void setWidth(int width) {width_ = width;}
  void setHeight(int height) {height_ = height;}
  int GetArea() const {return width_ * height_;}

private:
  // 定义初始化参数,不写的话自动给0
  int width_ = 10;
  int height_ = 5;  
};

int main() {// 初始化一个shape
    Shape shape;
    std::cout<< "默认初始化的width: "<< shape.GetWidth()<< std::endl;
    std::cout<< "默认初始化的Height: "<< shape.GetHeight()<< std::endl; 
    std::cout<< "默认初始化的Area: "<< shape.GetArea()<< std::endl; 
    return 0;
}
C++里面的this特殊指针
class Shape {public:
  Shape() = default;
  Shape(int width, int height) : width_(width), height_(height) {}
  int GetWidth() const {return width_; }
  void SetWidth(int width) {width_ = width; }
  void SetWidthThroughThis(int width) {this->width_ = width; }
  private:
  int width_;
};
Shape shape;
shape.SetWidth(5);
shape.SetWidthThroughThis(5);

使用 this->是一种好的编程习惯,它可以在代码中清晰地表示出当前访问的是对象的成员变量,而不是局部变量。这样可以避免命名冲突,提高代码的可读性。

另外,在某些情况下,使用 this->可以解决默认参数和局部变量之间的命名冲突。

总之,使用 this->是一种可以提高代码可读性和健壮性的好的编程习惯。

静态变量,函数
#includeclass Shape {public:
  Shape() = default;
  Shape(int width, int height) : width_(width), height_(height) {}
  int GetWidth() const {return width_; }
  void SetWidth(int width) {width_ = width; }
  // 静态变量
  static int GetTotalShapes() {return total_shapes_; }
  // 静态函数
  static void IncreaseTotalShapes() {total_shapes_++; }
 private:
  int width_;
  int height_;
  // 静态变量
static int total_shapes_;
};

int Shape::total_shapes_ = 0;

int main() {Shape shape1(3, 4);
Shape shape2(5, 6);
std::cout<< "Total Shapes: "<< Shape::GetTotalShapes()<< std::endl;
Shape::IncreaseTotalShapes();
Shape::IncreaseTotalShapes();
std::cout<< "Total Shapes: "<< Shape::GetTotalShapes()<< std::endl;
return 0;
}
友元函数/类
class MyClass {friend void MyFriendFunction(MyClass& my_class);
  private:
  int x_;
};

void MyFriendFunction(MyClass& my_class) {my_class.x_ = 5; // 访问类的私有成员变量
}
#includeusing namespace std;

class Box {public:
  // 声明友元函数
  friend void printWidth(Box& box);
  // 声明友元类
  friend class BigBox;
  // 保留默认构造函数
  Box() = default;

  // 自定义构造函数
  Box(int width): width_(width) {}

  void setWidth(float width) {width_ = width;}
  int getWidth() {return width_;}

private:
  float width_ = 5;
};

class BigBox{public:
  // 写一个函数更改变量
   void print(int width, Box& box) {  box.setWidth(width);
      cout<< "width of box: "<< box.width_<< endl;
   }
};

void Box_test() {Box box;
  cout<< "默认的width: "<< box.getWidth()<< endl;
  box.setWidth(6);
  cout<< "更改后的width: "<< box.getWidth()<< endl;
}

void BigBox_test() {Box box;
  BigBox bigbox;
  box.setWidth(9.0);
  bigbox.print(20, box);  // 这里就把Box的width_改了,不是改实例化出来的box
  cout<< "box width after friend class change: "<< box.getWidth()<cout<< "friend box width_ is: "<< box.width_<< endl;
}
int main() {// Box_test();
  // BigBox_test();
  Box box;
  printWidth(box);
  return 0;
}
类的继承
#includeclass Shape{public:
  // 保留默认构造函数
  Shape() = default;    
  Shape(int width, int height): width_(width), height_(height) {}

// 成员函数
public:
  void setWidth(int width) {width_ = width;}
  void setHeight(int height) {height_ = height;}

// 成员变量
protected:
  int width_ = 5;
  int height_ = 5;
};

class Rectangle: public Shape {public:
  // 成员函数
  int getArea() {// 继承的class Shape 的width_, height_
    return width_ * height_; 
  } 
};

int main() {Rectangle rect;
    // 继承了Shape就可以用他的成员函数
    rect.setHeight(10);
    rect.setWidth(10);
    std::cout<< "Area: "<< rect.getArea()<< std::endl;
    return 0;
}
多继承
#includeclass Shape{public:
  // 保留默认构造函数
  Shape() = default;    
  Shape(int width, int height): width_(width), height_(height) {}

// 成员函数
public:
  void setWidth(int width) {width_ = width;}
  void setHeight(int height) {height_ = height;}

// 成员变量
protected:
  int width_ = 5;
  int height_ = 5;
};

class Cost {public: 
    auto getCost(int area) {return area * 70;}
};

class Rectangle: public Shape, public Cost {public:
  // 成员函数
  int getArea() {// 继承的class Shape 的width_, height_
    return width_ * height_; 
  } 
};


int main() {Rectangle rect;
    // 继承了Shape就可以用他的成员函数
    rect.setHeight(10);
    rect.setWidth(10);
    std::cout<< "Area: "<< rect.getArea()<< std::endl;
    auto area = rect.getArea(); 
    auto cost = rect.getCost(area);
    std::cout<< "Cost of rect is: "<< cost<< std::endl;
    return 0;
}
菱形继承
//     A
        //    / \ 
        //   B   C 
        //   \   /
        //     D



#includeusing namespace std;

class A{public: 
  void func() {std::cout<< "Class A"<< std::endl;
  }
protected:
int base_;
};

class B: public A{public: 

  
protected:
int b_;
};

class C: public A{public: 

  
protected:
int c_;
};

class D: public B, public C{public: 

  
protected:
int d_;
};

int main() {// 实例化b
    B b;
    b.func(); 
    // 实例化d
    D d;
    d.func(); // 不可以访问了
    return 0;
}
d.B::func();
继承的一些特殊情况
#includeclass Shape{public:
  // 保留默认构造函数
  Shape() = default;    
  Shape(int width, int height): width_(width), height_(height) {}

// 成员函数
public:
  void setWidth(int width) {width_ = width;}
  void setHeight(int height) {height_ = height;}

// 成员变量
protected:
  int width_ = 5;
  int height_ = 5;
};

class Cost {public: 
    auto getCost(int area) {return area * 70;}
};

class Rectangle: public Shape, public Cost {public:
  // 成员函数
  int getArea() {// 继承的class Shape 的width_, height_
    return width_ * height_; 
  } 

  int getShapeArea() {return Shape::height_ * Shape::width_;
  }

private:
  int width_ =100;  // 嵌套变量的作用域:
  int height_ = 700;
};


int main() {Rectangle rect;
    // 继承了Shape就可以用他的成员函数
    rect.setHeight(10);
    rect.setWidth(10);
    std::cout<< "Area: "<< rect.getArea()<< std::endl;
    auto area = rect.getArea(); 
    auto area2 = rect.getShapeArea();
    auto cost = rect.getCost(area);
    auto cost2 = rect.getCost(area2);
    std::cout<< "Cost1 of rect is: "<< cost<< std::endl;
    std::cout<< "Cost2 of rect is: "<< cost2<< std::endl;
    std::cout<< "Cost2 of rect is: "<< sizeof(Shape)<< std::endl;
    
    
    return 0;
}
重载与重写
auto getCost(int area) {return area * 70;}
  
  void getCost() {std::cout<< "We are in getCost func in calss rect "<< std::endl;
    
  }
void getCost(int area) {std::cout<< "area is "<< area<< std::endl;}
  
  void getCost() {std::cout<< "We are in getCost func in calss rect "<< std::endl;
    
  }

你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧


当前标题:C++笔记(四)类和结构体,类的继承,多继承,菱形继承-创新互联
文章地址:http://mzwzsj.com/article/dgpgpj.html

其他资讯

让你的专属顾问为你服务