Classes

Hi,

I have just written my first class and I think it is working but no doubt I have made mistakes but as I am new to this I am not able to spot them yet.

I have written a class called Factoral which can be used to calculate a numbers factoral.

I was wondering if someone could take a look and advise me what mistakes I have made. I know that later I need to create constructors and destructors but I have not got to that yet.

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
//Author:       **********
//Date: 	25/11/2013
//Title:        Class Factoral
//Description:  Class that calculates fatcoral when eser inputs number and program uses function recursion to return factoral

#include <iostream>
#include <conio.h>

using namespace std;

class Factoral
{
     public://begin public section
            unsigned int get_number();//accessor member function
            void set_number(unsigned int number);//accessor member function
            unsigned int get_value ();
            void set_value ();
     private://begin private section
            unsigned int its_number;//member variable
            unsigned int its_value;//member variable
};

//Public accessor function which returns value of its_number member
unsigned int Factoral::get_number()
{
     return its_number;//returns value of member variable its_number
}

//Public accessor function which sets value of its_number member
void Factoral::set_number(unsigned int number)
{
     //unsigned int number;
     cout << "Enter your number: ";//user prompted to enter number
     cin >> number;
     its_number = number;//setting value of member variable its_number
}

//Public accessor function which returns value of its_value member
unsigned int Factoral::get_value()
{
     return its_value;
}

//Public accessor function which is used to set value for "its_value"
void Factoral::set_value ()
{
     if (its_number == 1 || its_number == 0)
        its_value = 1;
     else
     {
         unsigned int value = its_number;//declaring variable value that holds the value of the calculated factoral.
         unsigned int count = its_number;
         for (count; count > 1; count--)
         {
             value *= (count-1); 
         }
         its_value = value;
     }
}

int main ()
{
    unsigned int number = 0;//declaring variable number which is used to set value of its_number and is initialised with value of zero
        
    Factoral myFactoral;//creating object of class Factoral
    myFactoral.set_number(number);//calling member function to set value of member variable its_number
    myFactoral.set_value ();//calling member function to set value of member variable its_value
    cout << myFactoral.get_number() << "! is " << myFactoral.get_value() << endl;
    
    _getch ();//pauses program
    return 0;
}

Just a few things slightly unrelated:

You don't need to explicitly set the number to 0 in line 66, the automatic constructor for an int (initialized with the invisible constructor for your class) will do that for you. Also, your set_number function does not require you to pass it an int in the way you have it, as you are overwriting that number immediately anyway.
You don't need to explicitly set the number to 0 in line 66, the automatic constructor for an int (initialized with the invisible constructor for your class) will do that for you.

Do not listen to this advice. This is NOT something you can rely on. If you don't initialise a variable, then its value is undefined.

Now, some compilers, in some configurations, might initialise variables to 0. But that's not something you should ever rely on if you want to write robust code.
Hi NT3,

I have followed your advice and changed my program. I have updated the set_number () member function by removing its parameters and declaring number in the function. I have also removed the variable number from main ().

Does it look ok now?

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//Author:       *******
//Date: 	25/11/2013
//Title:        Class Factoral
//Description:  Class that calculates fatcoral using the number inputted by user

#include <iostream>
#include <conio.h>

using namespace std;

class Factoral
{
     public://begin public section
            unsigned int get_number();//accessor member function
            void set_number();//accessor member function
            unsigned int get_value ();
            void set_value ();
     private://begin private section
            unsigned int its_number;//member variable
            unsigned int its_value;//member variable
};

//Public accessor function which returns value of its_number member
unsigned int Factoral::get_number()
{
     return its_number;//returns value of member variable its_number
}

//Public accessor function which sets value of its_number member
void Factoral::set_number()
{
     cout << "Enter your number: ";//user prompted to enter number
     unsigned int number;
     cin >> number;
     its_number = number;//setting value of member variable its_number
}

//Public accessor function which returns value of its_value member
unsigned int Factoral::get_value()
{
     return its_value;
}

//Public accessor function which is used to set value for "its_value"
void Factoral::set_value ()
{
     if (its_number == 1 || its_number == 0)
        its_value = 1;
     else
     {
         unsigned int value = its_number;//declaring variable value that holds the value of the calculated factoral.
         unsigned int count = its_number;
         for (count; count > 1; count--)
         {
             value *= (count-1); 
         }
         its_value = value;
     }
}

int main ()
{
    Factoral myFactoral;//creating object of class Factoral
    myFactoral.set_number();//calling member function to set value of member variable its_number
    myFactoral.set_value ();//calling member function to set value of member variable its_value
    cout << myFactoral.get_number() << "! is " << myFactoral.get_value() << endl;
    
    _getch ();//pauses program
    return 0;
}
Last edited on
I was under the impression that a class variable had its constructor called upon initialization of the class, and the default constructor for an integer initializes to 0.

EDIT:
Yeah, that looks fairly good. The only thing I would add would be const identifiers, but you probably haven't learned them yet, so don't worry about it.
Last edited on
cheers mate for taking time to take a look.

I am just starting classes so not covered very much yet, have a lot to learn still. Think the next thing it covers is constructors and destructors.
I was under the impression that a class variable had its constructor called upon initialization of the class, and the default constructor for an integer initializes to 0.

This actually prompted quite a discussion here in the office. Those developers who've had reason in the past to investigate in detail what the standard actually says about this tell me that, no, the "invisible" default constructor for a class will not call the pseudo-default-constructor on data members which are of built-in types.

So, the standard does not dictate that, if you instantiate Factoral using the compiler-created default constructor, it initialises its_number and its_value to 0.

However, since this is therefore undefined behaviour, it's possible that any given implementation might do that.

In any case, the variable in the OP's code we were discussing was a local variable number which was declared in main, so the above isn't relevant anyway ;)

To the OP: it's probably better to post your updated code in a new post in the thread, rather than going back and editing your OP. It makes it easier to follow the discussions if we can see the original code that prompted those discussions.
Last edited on
> unsigned int get_number();//accessor member function

Make the accessors const-correct.
http://www.parashift.com/c++-faq-lite/const-member-fns.html

> I was under the impression that a class variable had its constructor called upon initialization of the class

unsigned int has a trivial default constructor; consequently the class Factoral also has a trivial default constructor. A trivial default constructor does nothing.

> and the default constructor for an integer initializes to 0.

No. However, any type with a trivial default constructor can be value initialized.
Factoral myFactoral{} ; will initialize the two member variables with literal zeroes.

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
#include <iostream>
#include <type_traits>
#include <cassert>

class Factoral
{
     public://begin public section
            unsigned int get_number() const { return its_number ; }
            void set_number(unsigned int number);//accessor member function
            unsigned int get_value() const { return its_value ; }
            void set_value ();
     private://begin private section
            unsigned int its_number;//member variable
            unsigned int its_value;//member variable
};

int main()
{
    std::cout << std::boolalpha
               << std::has_trivial_default_constructor<unsigned int>::value << '\n' // true
               << std::has_trivial_default_constructor<Factoral>::value << '\n' ; // true

    Factoral myFactoral{} ; // value initialized

    assert( myFactoral.get_number() == 0 && myFactoral.get_value() == 0 ) ;

    std::cout << myFactoral.get_number() << '\n' // 0
               << myFactoral.get_value() << '\n' ; // 0
}

http://ideone.com/t5hSvc
Topic archived. No new replies allowed.