+ 3
What's wrong?
4 Respostas
+ 7
I have made some changes in your code and now it is working properly :
#include <iostream>
#include <string>
using namespace std;
int k;
class myClass {
public :
int x,y,z;
int sum(){
cout<<"first number";
cin>>x;
cout<<"second number";
cin>>y;
cout<<"third number";
cin>>z;
k=x+y+z;
return k;
}
};
int main()
{
myClass ObjSum;
ObjSum.sum();
cout<<k;
}
+ 1
uhm....
I just moved the cout thing to Main() :
class myClass {
int x,y,z,k;
int sum() {
cin>>x;
cin>>y;
cin>>z;
k=x*y*z;
return k;
}
};
int main() {
myClass ObjSum;
int OOP = ObjSum.sum();
cout<<OOP;
}
avoid publicly declare variable
(because public declare seems not OOP)
+ 1
what makes class an Object Oriented is mostly because declaration is commonly private , not public ,
when you enter university , your Lecturer will teach those later on.
there's even Get and Set function , in your case :
int GetX(){ return x; }
int GetY(){ return y; }
int GetZ(){ return z; }
int GetK(){ return k; }
void SetX( int v ){ x=v; }
void SetY( int v ){ y=v; }
void SetZ( int v ){ z=v; }
void SetK( int v ){ k=v; }
inside that myClass
ask any University Lecturer , I dare you.
>:)
0
OK thanks, I see :d