zoukankan      html  css  js  c++  java
  • 面向对象_访问修饰符_构造与析构函数_this指针

    1:面向对象

     

     

     

     

     以codeblocks举例,在一个工程里面: File-->new -->Class可以建一个类,可以设置类的参数,是否有set get方法,有无构造函数等设置,.h文件主要用来写类的属性和

    方法声明,类名.cpp文件里面实现函数,main函数里面负责对象的调用和操作。 

    如下:Student.h

    #ifndef STUDENT_H
    #define STUDENT_H
    #include <bits/stdc++.h>
    using namespace std;
    class Student
    {
        public:
            Student();//构造函数,进行数据的初始化
            Student(string name, int age);
            virtual ~Student();
    
            string Getname() { return name; }//对类的变量进行封装,便于对私有的属性进行操作
            void Setname(string val) { name = val; }
            int Getage() { return age; }
            void Setage(int val) { age = val; }
            int* Getscore() { return score; }
            void Setscore(int* val) { score = val; }
            void Show(string , int );//在.h文件里面进行函数定义
        protected:
    
        private://私有的属性
            string name;
            int age;
            int* score;
    };
    
    #endif // STUDENT_H
    

     main.cpp

    #include <bits/stdc++.h>
    
    using namespace std;
    #include "Student.h"
    int main()
    {
        Student stu1;//;自动调用无参构造函数
        cout << "由构造函数初始化的年龄和姓名"  <<stu1.Getage() << stu1.Getname() << endl;
        Student stu2("小红",12);//有参构造函数
        stu1.Show("小明",13);
        return 0;
    }
    

    student.cpp

    /*
     * 文件名:
     * 描  述:
     * 作  者:
     * 时  间:
     * 版  权:
     */#include "Student.h"
    #include <bits/stdc++.h>
    using namespace std;
    Student::Student()
    {
        name = "默认名";
        age = 100;
        cout << "我是(无参)构造函数" << endl;
    }
    Student::Student(string name,int age){
        Student::Setage(age);
        Student::Setname(name);
        name = Student::Getname();
        age = Student::Getage();
        cout << "学生" <<name<< "的年龄是"  << age << endl;
    }
    
    Student::~Student()
    {
        cout << "我是析构函数,负责对象的回收" << endl;
    }
    void Student::Show(string name, int age)
    {
        cout << "学生" <<name << "的年龄是"  << age << endl;
    }
    

    2:访问修饰符

     通过为参数设置set和get方法进行私有属性的操作。

     1 #ifndef STUDENT_H
     2 #define STUDENT_H
     3 #include <bits/stdc++.h>
     4 using namespace std;
     5 class Student
     6 {
     7     public:
     8         Student();//构造函数,进行数据的初始化
     9         Student(string name, int age);
    10         virtual ~Student();
    11 
    12         string Getname() { return name; }//对类的变量进行封装,便于对私有的属性进行操作
    13         void Setname(string val) { name = val; }
    14         int Getage() { return age; }
    15         void Setage(int val) { age = val; }
    16         int* Getscore() { return score; }
    17         void Setscore(int* val) { score = val; }
    18         void Show(string , int );//在.h文件里面进行函数定义
    19     protected:
    20 
    21     private://私有的属性
    22         string name;
    23         int age;
    24         int* score;
    25 };
    26 
    27 #endif // STUDENT_H

    3:构造与析构函数

     

     

     

     .cpp文件

     1 /*
     2  * 文件名:
     3  * 描  述:
     4  * 作  者:
     5  * 时  间:
     6  * 版  权:
     7  */#include "Student.h"
     8 #include <bits/stdc++.h>
     9 using namespace std;
    10 Student::Student()
    11 {
    12     name = "默认名";
    13     age = 100;
    14     cout << "我是(无参)构造函数" << endl;
    15 }
    16 Student::Student(string name1,int age1):name(name1),age(age1)//初始化列表
    17 {
    18     //name = name1;
    19     //age = age1;
    20     name = Student::Getname();
    21     age = Student::Getage();
    22     cout << "学生" <<name<< "的年龄是"  << age << endl;
    23 }
    24 
    25 Student::~Student()
    26 {
    27 
    28     cout << "我是析构函数,负责对象"<< Student::Getname()<<"的回收" << endl;
    29 }
    30 void Student::Show(string name, int age)
    31 {
    32     cout << "学生" <<name << "的年龄是"  << age << endl;
    33 }
    View Code

    main.cpp文件

     1 #include <bits/stdc++.h>
     2 
     3 using namespace std;
     4 #include "Student.h"
     5 int main()
     6 {
     7     Student stu1;//;自动调用无参构造函数,在栈内存中分配空间,自动调用delete回收
     8     cout << "由构造函数初始化的年龄和姓名"  <<stu1.Getage() << stu1.Getname() << endl;
     9     Student stu2("小红",12);//有参构造函数
    10     stu1.Show("小明",13);
    11 
    12     Student*  stu3 = new Student("岳飞",40);//在堆空间中分配内存
    13     cout << "名字是" << stu3->Getname() << endl;
    14     delete stu3;//需要手动调用delete释放内存
    15 
    16     return 0;
    17 }
    View Code

     

    栈空间对象自动释放,通过new占用的堆空间需手工delete释放,析构函数只有一个,不能重载。

    4:this指针

     (*this)返回的是类对象本身。

    this的一个总结

  • 相关阅读:
    linux下git以及github的连接与使用
    在windows上如何安装python web引擎jinja2
    JS请求服务器并使页面跳转(转)
    Spring MVC中Session的正确用法<转>
    Eclipse上安装GIT插件EGit及使用
    深入理解JavaScript事件循环机制
    React Hooks useContext 进行父子组件传值
    Remove all your local git branches but keep master
    常见的web前端性能优化
    js知识梳理2:对象属性的操作
  • 原文地址:https://www.cnblogs.com/henuliulei/p/11701045.html
Copyright © 2011-2022 走看看