converting to a class

I've made a program to find the both roots using the quadratic equation, but I'm now told I have to use classes in the header file and get functions. I'm a bit confused as to how to do this. I know that the class should have public and private members but I don't know how to distribute them. This is my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <cmath>
using namespace std;


int main()
{
    double a,b,c, Discriminant;
    float Root1, Root2;

    cout << "\n" << "Please enter the a value: ";
    cin >> a;
    cout << "Please enter the b value: ";
    cin >> b;
    cout << "Please enter the c value: ";
    cin >> c;
	
    Discriminant = ((b*b) - 4*a*c);
    cout << "The discriminant is "<< Discriminant << endl;
    
    if (Discriminant < 0)
    {
	cout << "The equation has no real roots" << endl;
    }
    else if (Discriminant = 0)
    {
	Root1 = ((-1*b) + Discriminant / (2*a));
	cout << "The one real Root = " << Root1 << endl;
    }
    else
    {
	Root1 = ((-1*b) + Discriminant / (2*a));
	Root2 = ((-1*b) - Discriminant / (2*a));
	cout << "Root 1 = " << Root1 << endl;
	cout << "Root 2 = " << Root2 << endl;
    }
}
Example of Get and Set functions

1
2
3
4
5
6
7
8
9
inline CBase::GetFirstValue () const
{
  return aval;
}

inline void CBase::SetFirstValue (int fvalue)
{
  aval = fvalue;
}

1
2
3
4
5
6
7
8
//main.cpp
int main(){
      CBase base;
 //     cout << "\n" << "Please enter the a value: ";
 //     cin >> a;      
      base.SetFirstValue(firstinput);
      a =  base.GetFirstValue();
}

something like this I think...

Last edited on
The whole purpose behind using get's and sets is data hiding. So people can't directly manipulate your variables from other classes.
Realistically sets aren't needed so much as the constructor can set the variable by use of parameters. Making the variable private, and simply having a get method for classes not of this type will allow access.

1
2
3
4
5
6
7
8
9
10
11
12
// myclass.h

#include<iostream>

class myclass
{
   public:
      myclass(int theValue);
      int getValue();
   private:
      int value;
};


1
2
3
4
5
6
7
8
9
10
11
12
13
// myclass.cpp

#include "myclass.h"

myclass::myclass(int theValue)
{
   value = theValue;   //  this sets the private int value
}

int myclass::getValue()
{
   return value;  // this is the private int value that we can access via the method.
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// main

#include "myclass.h"
using std::cout;

int main()
{
   // create an object of the class
   myclass mc(653);

   // use the objects method
   cout << mc.getValue() << endl;

   return 0;
}


edit: TBH, I don't understand this much as if you create an object of a class then you have access to all it's members regardless, having sets and gets simply makes it slightly more difficult(slightly) to mess with the variable.... It's to do with good OO design apparently.

this is a decent link:
http://www.kirit.com/C%2B%2B%20killed%20the%20get%20&%20set%20accessors

The only thing I could find in google was about java:

Did some research and found the following:

accessor methods
Normally you don't make variables in your class public. Instead you provide public set and get
methods for the variable. This lets you then later change the way the variable is stored, or add
extra validation checks.

Method names made up of a member name with a get or set prefix are methods that are
required when the members are private and therefore cannot be accessed from outside the
class (which is good programming I understand). Any program that creates instances of a class
will use these methods to access the members. A member does not need a set method when its
value is changed in other methods.

The get/set methods are not compulsory, only include them for members when other classes
require access to them.

The get methods (also called an accessor method) return the value of the member they are
associated with and the set method (also called a mutator method) sets the member to a new
value.

Using get and set methods aids maintainability, flexibility and extensibility. Good design includes
encapsulation – to do this you make public asscessor methods and force calling code to use
those methods. I.e. set and get


This post also has a bit of info:
http://www.cplusplus.com/forum/general/6593/

If you haven't noticed a recurring theme in my post yet it is: get/set's are pretty useless in C++, but still remain good in C# java etc...
C++ you should still use the idea of encapsulation and the first link I posted explains it well, but there's better ways to do it rather than sets/gets..

:) Have fun reading. I hope I didn't dump too much on you, you can't handle.
Just for the meanwhile do as your teacher expects, just remember there's a bigger better world out there...
Last edited on
@gcampton
get/set's are pretty useless in C++,
??

But it is still used by experts.
They use get/set's method when they declare the methods inline or virtual.

This is an example code.
1
2
3
4
5
6
7
8
9
// Class CWeighing_Ctrl 

//## Get and Set Operations for Associations (inline)

inline CWghPrinter& CWeighing_Ctrl::GetWghPrint ()
{
  return theWghPrint;
}
Maybe I should write an article about this....

the rule that "no member should be public" is utter crap. get/set functions definitely have their place and you should employ them where necessary... which is a lot of the time. Probably even most of the time. Just not all of the time.

If your get function does nothing but return the value, and your set function does nothing but assign the value, then your get/set is pointless and you might as well make the member public.


@olredixis:

that example code is the perfect example of the worst kind of get/setter. It defeats the ENTIRE point of having get/set functions.
Yeah write an article about this...

I would like to know more about this.

I have a job now and I am still in the training period.

I red the link posted by gcampton.

I was scanning the codes (.cpp files and .h files),
and noticed this get/set functions.

I was wondering about the reasons why they used get and set methods.

So should I avoid using get/set functions?

Thanks.

EDIT:
@gcampton
get/set's are pretty useless in C++, but still remain good in C# java etc...


So why is still good in java and C#?
They also use encapsulation right?

so what are the specific examples in using get/set method in Java or C# to be clear.

Thanks.
Last edited on
Yeah write an article about this...


I might give it a go later tonight. No promises.

I've also been thinking of writing an article on encapsulation, but that's pretty tricky.

I was wondering about the reasons why they used get and set methods.


A lot of people do it simply because they have it in their head that members should never be public.

I obviously disagree with that way of thinking... but it is still very common.

So should I avoid using get/set functions?


No. The only real downside to them (provided they're inlined) is that they require more typing and they make your code a few lines longer.

It's not that they're "bad", it's just that they're sometimes unnecessary. Why make yourself jump through hoops if you don't have to?
Ok thanks Disch, I will look forward to your article and hope you will finish it tonight..
Topic archived. No new replies allowed.