美文网首页
ndk-C++基础进阶

ndk-C++基础进阶

作者: mirocle_cheng | 来源:发表于2019-07-29 21:51 被阅读0次

类的构造、析构、友元函数

Student.h

#ifndef Student_H
#define Student_H
class Student {
  friend void test(Student*);
  friend class Teacher;
  int i;
public:
  Student(int i, int j);
  ~Student(); // 析构函数
  void setJ(int j) const;
  int getJ() {
    return j;
  }
private:
  int j;

protected:
  int k;
public:
  int l;
};
//友元类
class Teacher {
public:
    void call(Student* s) {
      s->j = 10086;
    }
};
#endif // !Student_H

Student.cpp

#include "Student.h"
#include<iostream>
using namespace std;
Student::Student(int i, int j):i(i) {
  this->i = i;
  cout << "构造方法" << endl;
}

//常量函数
//表示不会 也不允许  去修改类中的成员
void student::setJ(int j) const {
}

test.cpp*

#include<iostream>
#include "Student.h"
void test(Student* stu) {
  stu->j = 100;
}
int main() {
  Student student(10, 20);
  test(&student);
  std::cout << "Hello World!\n";
  std::cout << student.getJ() << endl;
}
Student::~Student() {
  cout << "析构方法" << endl;
}

相关文章

网友评论

      本文标题:ndk-C++基础进阶

      本文链接:https://www.haomeiwen.com/subject/lcoirctx.html