#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
class Movie{
string title;
string genre;
int rating;
public:
Movie(string t, string g);
void printDetail();
};
Movie :: Movie(string t, string g){
title = t;
genre = g;
rating = rand()%10;
}
void Movie :: printDetail(){
cout <<" Title : " <<title<<endl;
cout <<" Genre : " <<genre<<endl;
cout <<" Rating : " <<rating<<endl;
}
在main
中,我有以下内容:
int main()
{
Movie M[5]={
Movie("The Avengers","Action"),
Movie("Infinity War","Action"),
Movie("Gupigain Baghabain","Comedy"),
Movie("Anonymous 616","Horror"),
Movie("Sara's Notebook","Thriller")
};
for(int i=0;i<5;i++){
cout <<" "<<i+1<<".";
M[i].printDetail();
cout<<endl;
}
M[0].rating; // Here i need to access to rating...!!
// Here I need to compare which movies rating is highest..!!
// So I need to access rating variable...!!
// But how can I do this...!!
// There has a condition... That is I can't declare any other member function..!!!
}
在这里,我必须显示哪个人的评分最高,哪个人的评分最低.. !!
答案 0 :(得分:2)
将此添加到课程中
bool isHighest(Movie otherMovie){
return rating > otherMovie.rating;
}
如果需要,可以使其成为运算符:
bool operator >(Movie otherMovie){
return rating > otherMovie.rating;
}
答案 1 :(得分:0)
您可以使用getter和setter函数来间接访问私有成员 还有另一种非标准的技巧:将私有成员的最讨厌的东西带到班级负责人,然后获取该类的地址并添加它的最坏的东西,这样您便拥有了私有成员的地址并可以直接访问它 标准c ++不支持此功能,您需要自己进行计算,这并不难 当您想从dll中获取结构或类以获取特定成员的值并且您没有该结构的定义但可以使用诸如反汇编之类的各种方法从dll中获得rva时,可以使用此方法dll的一些字节