C语言面向结构,C++面向对象
    栈对象生命周期函数体内部,堆对象生命周期可自己决定new的要自己delete。用new创建的是堆对象UJ[J_W{7E07I2T{G}]AKF72.png

    1. #include <iostream>
    2. using namespace std;
    3. int main()
    4. {
    5. double r, h, v;
    6. cin >> r >> h;
    7. v = 3.14 * r * r * h;
    8. cout << v << endl;//endl相当于换行
    9. return 0;
    10. }

    E977I(Y6KF2G`R1SQ$HFQ@2.png
    public公有权限,private私有权限,protected保护权限
    成员函数——成员方法;
    访问权限一般原则:数据成员private;成员函数public;

    1. #include <iostream>
    2. using namespace std;
    3. class Cup {
    4. private:
    5. double radius;
    6. double height;
    7. public:
    8. //构造函数,以类的方式创建类对象,编译器自动调用构造函数完成变量初始化
    9. Cup(double r, double h) {
    10. radius = r;
    11. height = h;
    12. }
    13. double getvolume()//普通成员函数,计算体积
    14. {
    15. return 3.14 * radius * radius * height;
    16. }
    17. };
    18. int main() {
    19. double r, h;
    20. cin >> r >> h;
    21. Cup c(r, h);//以类的方式构造类对象,栈对象
    22. cout << c.getvolume() << endl;//打印输出并换行
    23. return 0;
    24. }
    1. #include <iostream>
    2. #include <cstring>
    3. using namespace std;
    4. class Student
    5. {
    6. public:
    7. void set_data(int n, char *p,char s);
    8. void display( );
    9. private:
    10. int num;
    11. char name[20];
    12. char sex;
    13. };
    14. void Student::set_data(int n, char *p,char s)//在类外进行了实现构造函数
    15. {
    16. num=n;
    17. strcpy(name,p);
    18. sex=s;
    19. }
    20. void Student::display( )
    21. {
    22. cout<<"num: "<<num<<endl;
    23. cout<<"name: " <<name<<endl;
    24. cout<<"sex: " <<sex<<endl<<endl;
    25. }
    26. int main()
    27. {
    28. Student stud1,stud2;
    29. stud1.set_data(1,"He",'f');
    30. stud2.set_data(2,"She",'m');
    31. stud1.display();
    32. stud2.display();
    33. return 0;
    34. }

    FFBQCPI_~39X5[IY_D45)NM.png](https://cdn.nlark.com/yuque/0/2021/png/12500589/1615181073982-45e233ee-37bd-4ca6-8be4-46b0132afdbc.png#align=left&display=inline&height=1039&margin=%5Bobject%20Object%5D&name=FFBQCPI_~39X5%5BIY_D45%29NM.png&originHeight=1039&originWidth=1920&size=226033&status=done&style=none&width=1920)<br />如果遇到strcpy安全函数报错,可以将SDL检查改为否。<br />![EJFN_})(5TD~A%XSCRC]%MY.png
    访问对象中成员的一般形式 对象名.成员名
    通过指针访问对象中的成员 p->show_time( );

    1. #include <iostream>
    2. using namespace std;
    3. class Time
    4. {
    5. public:
    6. Time() //类内定义构造函数
    7. {
    8. hour = 0;
    9. minute = 0;
    10. sec = 0;
    11. }
    12. void set_time();
    13. void show_time();
    14. private:
    15. int hour;
    16. int minute;
    17. int sec;
    18. };
    19. void Time::set_time()
    20. {
    21. cin >> hour;
    22. cin >> minute;
    23. cin >> sec;
    24. }
    25. void Time::show_time()
    26. {
    27. cout << hour << ":" << minute << ":" << sec << endl;
    28. }
    29. int main()
    30. {
    31. Time t1;
    32. t1.show_time();
    33. Time t2;
    34. t2.set_time();
    35. t2.show_time();
    36. return 0;
    37. }

    T]352Y~5BAX7G$A`(X)OS~O.png
    输入小时分钟秒,显示小时分钟秒
    当以类的方式创建类对象时,初始化时自动调用构造函数。
    构造函数不需要用户来调用它,而是在建立对象时自动执行。
    构造函数的名字必须与类名同名,而不能由用户任意命名。
    构造函数不具有任何类型,不返回任何值。
    构造函数的功能是由用户定义的,用户根 据初始化的要求设计函数体和函数参数。
    当对象生命周期结束时,需要释放内存空间时自动调用析构函数
    析构函数的作用在撤销对象占用的内存之前完成一些清理、善后的工作。
    只要对象的生命期结束,程序就自动执行事先设计好的析构函数来完成相关工作。
    执行了几个构造函数,就要执行几次析构函数
    QString字符串类型

    1. #include <iostream>
    2. using namespace std;
    3. #include <string>
    4. class Student
    5. {
    6. public:
    7. Student(int n, string nam, char s)
    8. {
    9. num = n;
    10. name = nam;
    11. sex = s;
    12. cout << "执行构造函数:" << name << " come." << endl;
    13. }
    14. ~Student()
    15. {
    16. cout << "执行析构函数:" << "Bye bye, " << name << "." << endl;
    17. }
    18. void display();
    19. private:
    20. int num;
    21. string name;
    22. char sex;
    23. };
    24. void Student::display()
    25. {
    26. cout << "num: " << num << endl;
    27. cout << "name: " << name << endl;
    28. cout << "sex: " << sex << endl << endl;
    29. }
    30. int main()
    31. {
    32. Student stud1(10010, "Wang_li", 'f');
    33. stud1.display();
    34. Student stud2(10011, "Zhang_fun", 'm');
    35. stud2.display();
    36. return 0;
    37. }

    若提示name前的<<字符无法识别报错,解决办法添加头文件#include

    1. #include <QApplication>
    2. int main(int argc,char* argv[])
    3. {
    4. QApplication a(argc,argv);
    5. return a.exec();
    6. }