Main functions, int & OOP

Hi, this is pretty basic, but i have no idea how to fix the bug i have.
I can't manage to get a int value in my objects in my main.
Is there a way you could tell me how to get my customerAccount1 to get the value 25 and get it tested by the setAccountResult. I'll show you the code, i use GNU C++ compiler and code::block as an ide

here'S the code
------------------------------------------------------------------------
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//HEADER
#include <iostream>

using namespace std;


class CustomerAccount
{

public:
      CustomerAccount( int balance );
      void setAccountResult( int balance );
      int getAccountResult();
private:
    int balance;
};

------------------------------------------------------------------------
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
//CONSTRUCTOR
#include <iostream>
#include "CustomersAccount.h"
using namespace std;


int CustomerAccount::CustomerAccount( int balance )
{
    setAccountResult ( balance );
}

void CustomerAccount::setAccountResult ( int balance )
{
    if ( balance >= 0 )
    accountResult = balance;

    if (balance < 0 )
    cout << balance << "is not a valid number" << endl;
}

int CustomerAccount::getAccountResult ( int balance )

{
    return accountResult;
}

------------------------------------------------------------------------
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//MAIN
#include <iostream>
#include "CustomersAccount.h"

using namespace std;


int main()
{

    CustomerAccount customerAccount1( int balance );



    customerAccount1.setAccountResult( int balance )
    
    
    cout << customerAccount1.getAccountResult( int balance ) << endl;

}
Last edited on
If you will set balance just after you create the object;

This is in main:
1
2
3
4
CustomerAccount customerAccount1; 
customerAccount1.setAccountResult( 25);

cout << customerAccount1.getAccountResult( ) << endl;


This is constructor:
1
2
3
int CustomerAccount::CustomerAccount( ):balance(0) //initially makes zero
{
}

Change constructor declarator accordingly.Also,change getAccountResult to no argument.
Btw,there is no accountResult data in class declaration?



Last edited on
Because of the way he's coded the constructor, he can actually assign values on creation on the object, i.e.
CustomerAccount customerAccount1( 234 );


@isenhart

When you want to pass variables to functions, you've already declared that you will be passing an 'int', so you don't need to have ( int balance ) within main.

Also, in main, you haven't yet declared a variable called balance.

Depending on where you will get your data for that variable, you could do something like the following:
1
2
3
4
5
int balance = 0;
std::cout << "Please enter balance: "
std::cin >> balance;

customerAccount1( balance );


Also, in the int CustomerAccount::CustomerAccount( int balance ) function, instead of calling another function to assign the data for accountResult, you could just write: accountResult = balance;. Although, you don't have a variable called accountResult within the class?

You also don't have a destructor, not sure if that will throw any errors though.



And could someone tell me the difference of these two? I always do the first version.
1
2
3
4
5
6
7
//Constructor
foo( int integer = 0 );

foo::foo( int i )
{
    integer = i;
}

...And...

 
foo::foo():integer( 0 )
Also, in the int CustomerAccount::CustomerAccount( int balance ) function, instead of calling another function to assign the data for accountResult, you could just write: accountResult = balance;. Although, you don't have a variable called accountResult within the class?


He's calling it (probably) because it has a purpose: to validate the number you put in.

You also don't have a destructor, not sure if that will throw any errors though.


He'll be fine, the default destructor is perfect for this class.

And could someone tell me the difference of these two? I always do the first version.


The first one is "valid" assuming that the //constructor part is inside the class.
The second one one needs some { } after it, and is more efficent since it calls the int constructor directly.

The first one could be written as:
1
2
3
4
foo::foo( int i ) : integer() //default construction first
{
    integer = i; //then assignment
}


Whereas the second simply constructs the int directly.
Yeah, I always change the default constructor from (void), to having the varibles assigned.

Could you please explain as to what you mean by calling the int constructor directly?
I just looked through the reference on this site and a bit through google, but I'm not quite sure what to search for. And thanks.
Let's say you have a class like this:

1
2
3
4
5
class Something {
    std::string data;
public:
    Something(const std::string& data_);
};


If you do it the first way (no intializer list) then you are calling the default constructor first, then assigning the memory
1
2
3
4
5
Something::Something(const std::string& data_) : data() //initializer list automatically calls default
//constructor if you didn't specify one
{
    data = data_; //then assign
}


In this case the default constructor will set up the variables (like the size of the string and the memory), but then the assignment will basically reset everything with because of the new data. Hence you are wasting your type with the default construction. The better solution would be to do this:

1
2
3
Something::Something(const std::string& data_) : data(data_) //use the constructor to construct data here from data_
{//no code here because of above
}


This means you only construct data once with all the information, and no assignment. Hence, it is more efficient.
Oh, I see! I like that way much better already, ahaha.

Thanks for the reply.
No problem.
wow thanks for thoes reply, i will have some reading to do,

really thanks a lot!!!
Here's the answer, i've made it more complicated but it comes down to the same theory for the interested. This is pure basic Object Oriented programming.

-----------------------------
Constructor

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
#include <iostream>
#include "Account class.h"
using namespace std;


CustomerAccount::CustomerAccount ( int pBalance, int pQuantity, string pName, string pDescription  )
{

    balance=0;
    quantity=0;
    name="";
    description="";

    setAccountBalance( pBalance );
    setAccountQuantity( pQuantity);
    setAccountName( pName );
    setAccountDescription( pDescription );
}


void CustomerAccount::setAccountBalance( int pBalance)
{

    balance=pBalance;

}



int CustomerAccount::getAccountBalance()
{
    return balance;
}


void CustomerAccount::setAccountQuantity( int pQuantity)
{

    quantity=pQuantity;

}

int CustomerAccount::getAccountQuantity()
{
   return quantity;
}

void CustomerAccount::setAccountName( string pName )
{

    name=pName;

}

string CustomerAccount::getAccountName()
{
    return name;
}

void CustomerAccount::setAccountDescription( string pDescription )
{

    description=pDescription;
}

string CustomerAccount::getAccountDescription()
{
    return description;
}


---------------------------------
Header
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 <string>

using namespace std;


class CustomerAccount
{

public:
      CustomerAccount( int, int, string, string);

      void setAccountBalance( int );
      int getAccountBalance();

      void setAccountQuantity( int );
      int getAccountQuantity();

      void setAccountName( string );
      string getAccountName();

      void setAccountDescription( string );
      string getAccountDescription();


private:
    int balance, quantity;
    string name, description;
};


------------------------------------

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include "Account class.h"

using namespace std;



int main()
{


    CustomerAccount Item( 5, 1, "marteau", "sa frappe" );
    CustomerAccount Item2( 23536478, 777, "Bardeau" ,"C cher en sale" );
    CustomerAccount Item3( 123, 3, "Diaman", "nice deal" );
    cout << "Nom: " << Item.getAccountName() << "\nPrix de l'item en question: " << Item.getAccountBalance() << "\nnombre: "<<Item.getAccountQuantity() << "\nDescription: "<< Item.getAccountDescription() << endl;
    cout << "Nom: " << Item2.getAccountName() << "\nPrix de l'item en question: " << Item2.getAccountBalance() << "\nnombre: "<<Item2.getAccountQuantity() << "\nDescription: "<< Item2.getAccountDescription() << endl;
    cout << "Nom: " << Item3.getAccountName() << "\nPrix de l'item en question: " << Item3.getAccountBalance() << "\nnombre: "<<Item3.getAccountQuantity() << "\nDescription: "<< Item3.getAccountDescription() << endl;

}

I now understand OOP and that the clearer way to make it. Can't believe some book amkes it so complicated.
I now understand OOP


You probably don't.
There's two ways of making the constructor so that you don't have to call the functions as soon as you create an object.

The following is which I used to do:

1
2
3
4
5
6
7
CustomerAccount::CustomerAccount ( int pBalance, int pQuantity, string pName, string pDescription  )
{
    balance = pBalance;
    quantity = pQuantity;
    name = pName;
    description = pDescription ;
}


And then have this in public:
CustomerAccount( int b = 0, int q = 0, string = "undefined", string = "undefined");


And then there's the way FireDraco said to do it( when I can get it to work, lol ), which is more efficient. And which I need help with, ahaha. Anyone got a link to the way FireDraco did it, I'm having a few problems with the following code:
test( int integer, char *name );

1
2
3
4
5
test::test( int i, char *n ) : integer( 0 ), name( "undefined" )
{
	name = n;
	integer = i;
}


I'm getting:
error C2512: 'test' : no appropriate default constructor available.


hanst99 wrote
I now understand OOP

You probably don't.


lol, everytime I think I know something, I need to read more on it!
Last edited on
error C2512: 'test' : no appropriate default constructor available.

You probably have something like this:
test a; somewhere.
Yeah, I do. But I thought by initializing the constructor with:
test::test( int i, char *n ) : integer( 0 ), name( "undefined" )

...Meant that I could create an object without params.

Kind of the same as
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class...
private:
test::test( int integer = 0, char * name = "undefined" )

test::test( int i, char *n )
{
    integer = i;
    name = n;
}

int main()
{
    test t;

    return 0;
}


Do you know of a link, where I could read up on this, I've looked but only seem to find stuff with the default constructor unchanged.
test::test( int i, char *n ) : integer( 0 ), name( "undefined" )

The bold part is the initialization list - it's there for initializing stuff, you still have to abide the same calling conventions as with any other function. What you probably meant to do is:
test::test( int i = 0, const char *n ="undefinded" ) : integer( i ), name( n )
I'm still getting the same error, from editing my previous code, to the code above. Maybe I'm being dumb... ahaha.

It just wont construct the object without params.

Edit:
wow, yeah, I was being really dumb, ahaha. It took me a while to see it, but this line:
1
2
3
class...
private:
test::test( int integer = 0, char * name = "undefined" )


Was actually
1
2
3
class...
private:
test::test( int integer, char * name )



Thanks for the help, Hanst.
Last edited on
Ok, i'll edit my tell.

I now understand the basic concept of OOP!
Try cout<< "hi" <<endl;
Topic archived. No new replies allowed.