Jump to content

C++, Need help with OOP

BigCake
Go to solution Solved by Unimportant,
20 minutes ago, Atalia Chez said:

I dont understand one thing. Why did you put int x,y after creating class Mathematics and before void input function. 

 

In the following code:

class Mathematics 
{
  int x, y;
 
public:
  void input() {
    cout << "Input two inetegers\n";
    cin >> x >> y;
  }
 
  void add() {
    cout << "Result = " << x + y;
  }
};

Nothing is "created" yet. It is the definition of the Mathematics class, describing what the class looks like and what it can do, the declaration, and the implementation of the member functions, which makes it a definition. No object of the class is created, or instantiated,  yet.

 

In OOP, a class is meant to be a black box, a object that performs a certain functionality through its member functions. However, the actual internal implementation of such a class is meant to be hidden from the user.

As a simple analogy, lets take a television set. The television class offers functionality to the user: one can turn it on and off, change channels, etc. But the internals are hidden inside the casing.

 

To offer this same functionality in OOP, a class can have private members, only accessible from inside the class and "invisible" to the outside, and public members, which are accessible from the outside and form the class's interface.

 

The default for a class in C++ is private, and one can switch between public and private using the keywords:

class Example
{
	//Any declarations here are private!
  
public:
  
  	//Any declarations here are public!
  
private:
  
  	//Private again!
};

Thus, in @vorticalbox's example code, member variables x and y are private class members, only accessible from inside the Mathematics class and 'invisible' to the outside. Member functions input and add are public, and thus are accessible from the outside. input reads 2 integers from stdin and stores them as x and y inside the class's instance for which the member function is called. add adds both numbers together and prints the result to stdout.

 

int main()
{
   Mathematics m; // Creating object of class
 
   m.input();
   m.add();
 
   return 0;
}

The above piece of code is where a instance of the Mathematics class, called m, is actually created. One can create multiple instances of a class (try it) and each will have their own x and y inside. The code then simply calls both member functions on the instance.

Here is the code that I have just made. Dont know what the problem is. I  think I did everything right?

#include <iostream>
using namespace std;

class CLASS{
	
	public:
	int sum(int num1, int num2)
	{
		int result=0;
		
		cout << "1st num" << endl;
		cin >> num1;
		
		cout << "2nd num" << endl;
		cin >> num2;
		
		result= num1+num2;
		
		cout << "Total is: " << result << endl;
		
		return result;
	}
	
};


int main(){
	
	CLASS add;
	add.sum();
	
	
	return 0;
}

 

I just recently learned about classes and objects so please forgive me if there is some dumb mistake :3

 

Link to comment
Share on other sites

Link to post
Share on other sites

You need to pass numbers tot he function

 

add.sum(1,2);

 

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, vorticalbox said:

You need to pass numbers tot he function

 


add.sum(1,2);

 

He's scanning 2 numbers to sum from stdin tough, overwriting the arguments passed to the function. Probably better to just have the function take no arguments and define a local num1 and num2 in the sum function.

Link to comment
Share on other sites

Link to post
Share on other sites

19 minutes ago, Unimportant said:

He's scanning 2 numbers to sum from stdin tough, overwriting the arguments passed to the function. Probably better to just have the function take no arguments and define a local num1 and num2 in the sum function.

You are right. Is there any way to make it user accessible, meaning a user can put in his/her desired values?

 

If I pre define values for num1 and num2, it works.

Link to comment
Share on other sites

Link to post
Share on other sites

This works.

 

#include <iostream>
 
using namespace std;
 
class Mathematics {
  int x, y;
 
public:
  void input() {
    cout << "Input two inetegers\n";
    cin >> x >> y;
  }
 
  void add() {
    cout << "Result = " << x + y;
  }
 
};
 
int main()
{
   Mathematics m; // Creating object of class
 
   m.input();
   m.add();
 
   return 0;
}

 

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

12 minutes ago, vorticalbox said:

This works.

 

 

I dont understand one thing. Why did you put int x,y after creating class Mathematics and before void input function.  Here is what I understand for now:

 

1. Anything created before public can not be used in main function.

2. How come void input function not give error because that x and y variable are not in scope of input function?

 

I have been working with functions but every time if I initialized anything before function it would say "...not declared in scope".

Link to comment
Share on other sites

Link to post
Share on other sites

21 minutes ago, Atalia Chez said:

Why did you put int x,y after creating class Mathematics and before void input function.

To declare variables to be used in the class.

21 minutes ago, Atalia Chez said:

Anything created before public can not be used in main function.

Correct they are variables only accessible to the methods in the class

 

22 minutes ago, Atalia Chez said:

How come void input function not give error because that x and y variable are not in scope of input function?

Becuase it's not returning anything thus void, it's the same in c#. 

 

public void add(){

}

 

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

20 minutes ago, Atalia Chez said:

I dont understand one thing. Why did you put int x,y after creating class Mathematics and before void input function. 

 

In the following code:

class Mathematics 
{
  int x, y;
 
public:
  void input() {
    cout << "Input two inetegers\n";
    cin >> x >> y;
  }
 
  void add() {
    cout << "Result = " << x + y;
  }
};

Nothing is "created" yet. It is the definition of the Mathematics class, describing what the class looks like and what it can do, the declaration, and the implementation of the member functions, which makes it a definition. No object of the class is created, or instantiated,  yet.

 

In OOP, a class is meant to be a black box, a object that performs a certain functionality through its member functions. However, the actual internal implementation of such a class is meant to be hidden from the user.

As a simple analogy, lets take a television set. The television class offers functionality to the user: one can turn it on and off, change channels, etc. But the internals are hidden inside the casing.

 

To offer this same functionality in OOP, a class can have private members, only accessible from inside the class and "invisible" to the outside, and public members, which are accessible from the outside and form the class's interface.

 

The default for a class in C++ is private, and one can switch between public and private using the keywords:

class Example
{
	//Any declarations here are private!
  
public:
  
  	//Any declarations here are public!
  
private:
  
  	//Private again!
};

Thus, in @vorticalbox's example code, member variables x and y are private class members, only accessible from inside the Mathematics class and 'invisible' to the outside. Member functions input and add are public, and thus are accessible from the outside. input reads 2 integers from stdin and stores them as x and y inside the class's instance for which the member function is called. add adds both numbers together and prints the result to stdout.

 

int main()
{
   Mathematics m; // Creating object of class
 
   m.input();
   m.add();
 
   return 0;
}

The above piece of code is where a instance of the Mathematics class, called m, is actually created. One can create multiple instances of a class (try it) and each will have their own x and y inside. The code then simply calls both member functions on the instance.

Link to comment
Share on other sites

Link to post
Share on other sites

On a technical note, it's better to perform the user input in the main function than the class.

Aside from making your code easier to port to other user interfaces, replace components without having to touch your user interface code and reuse components within components that don't even have a user interface, all of which itself is a huge benefit, it also makes testing and debugging substantially easier, as you can isolate components entirely from the user input and test them one at a time by merely passing values at them, etc.

 

Thus, I hope you'll consider refactoring your program, or perhaps even challenging yourself to design a class that fulfills a realistic usecase with the above concerns in mind. If you're struggling for ideas, there's a tonne of exercises on the internet...

 

H-99: Ninety-Nine Haskell Problems is intended for Haskell/Lispy languages, but if you're eager to learn how to design class interfaces, designing function interfaces is a great place to start...

Project Euler will challenge your logical skills considerably!

I've never tried this website before, but it seems like there are some nice exercises there, too! I can imagine designing a class interface that doesn't violate DIP for the "permutations of strings" exercise could be quite enlightening :)

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×