12th Sep 2017, 7:51 AM
Jimmy Pub
Jimmy Pub - avatar
4 odpowiedzi
+ 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; }
12th Sep 2017, 8:05 AM
Lakshay
Lakshay - avatar
+ 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)
12th Sep 2017, 8:42 AM
Mikhael Anthony
Mikhael Anthony - avatar
+ 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. >:)
12th Sep 2017, 8:51 AM
Mikhael Anthony
Mikhael Anthony - avatar
0
OK thanks, I see :d
12th Sep 2017, 8:06 AM
Jimmy Pub
Jimmy Pub - avatar