AvP*_*AvP 0 c++ implementation class
我正在使用单独的编译进行家庭作业,我有一个关于访问我创建的类的数据成员的问题.当实现不带任何参数的类的成员函数并且我需要访问该类的数据成员时,我将如何在C++中执行此操作?我知道在Java中,有一个this关键字指的是调用该函数的对象.
我的头文件:
#ifndef _POLYNOMIAL_H
#define _POLYNOMIAL_H
#include <iostream>
#include <vector>
class Polynomial {
private:
std::vector<int> polynomial;
public:
// Default constructor
Polynomial();
// Parameterized constructor
Polynomial(std::vector<int> poly);
// Return the degree of of a polynomial.
int degree();
};
#endif
Run Code Online (Sandbox Code Playgroud)
我的实施文件:
#include "Polynomial.h"
Polynomial::Polynomial() {
// Some code
}
Polynomial::Polynomial(std::vector<int> poly) {
// Some code
}
int degree() {
// How would I access the data members of the object that calls this method?
// Example: polynomialOne.degree(), How would I access the data members of
// polynomialOne?
}
Run Code Online (Sandbox Code Playgroud)
我能够直接访问私有数据成员polynomial,但我想知道这是否是访问对象的数据成员的正确方法,还是我必须使用类似于Java的this关键字来访问特定对象数据成员?
应将此函数定义为Polynomial使用Polynomial::前缀的成员.
int Polynomial::degree() {
}
Run Code Online (Sandbox Code Playgroud)
然后,您可以访问成员变量,如矢量polynomial.