Reputation: 15
I'm very very new to coding C++ I'm learning OOP in C++ and I was trying to write a program on my own
class Account
{
private:
string name;
double balance;
public:
Account();
Account(string name_val);
Account(double bal_val);
Account(string name_val, double bal_val);
};
That's the class
Account::Account(): name { "None" }, balance{ 0.0 }{}
Account::Account(string name_val) : name{ name_val }, balance{ 0.0 }{}
Account::Account(double bal_val) : name{ "None" }, balance{ 0.0 }{}
Account::Account(string name_val, double bal_val) : name{ name_val }, balance{ bal_val }{}
These are the declarations that I did
Account empty;
Account frank{ "Frank" };
Account rich_boi{ 1000000.0 };
Account sad_boi{ "Sad boi", -10.0 };
These are the objects I made in main
Edit: I also did one program(which I got from a udemy course) before this which worked perfectly This was the class
class Player
{
private:
string name;
int health;
int xp;
public:
//Overloaded Constructors
Player();
Player(string name_val);
Player(string name_val, int health_val, int xp_val);
};
These were the constructors
Player::Player() : name{ "None" }, health{ 0 }, xp{ 0 }{}
Player::Player(string name_val) : name{ name_val }, health{ 0 }, xp{ 0 }{}
Player::Player(string name_val, int health_val, int xp_val) : name{ name_val }, health{ health_val }, xp{ xp_val }{}
And these were the objects in main
Player empty;
Player frank{ "Frank" };
Player villain{ "Villain" , 100 , 55 };
Upvotes: 1
Views: 298
Reputation: 5786
I am adding the full code which is executing successfully on my machine. Copy and execute to see if the code executes succesfully on your end.
#include<iostream>
using namespace std;
class Account
{
private:
string name;
double balance;
public:
Account();
Account(string name_val);
Account(double bal_val);
Account(string name_val, double bal_val);
};
Account::Account(): name ( "None" ), balance( 0.0 ){}
Account::Account(string name_val) : name( name_val ), balance(0.0 ){}
Account::Account(double bal_val) : name("None" ), balance(bal_val ){}
Account::Account(string name_val, double bal_val) : name( name_val ), balance( bal_val ){}
int main(){
Account empty;
Account frank("Frank" );
Account rich_boi( 1000000.0 );
Account sad_boi( "Sad boi", -10.0 );
}
Upvotes: 0
Reputation: 5786
Account::Account(): name ( "None" ), balance( 0.0 ){}
Account::Account(string name_val) : name( name_val ), balance(0.0 ){}
Account::Account(double bal_val) : name("None" ), balance(bal_val ){}
Account::Account(string name_val, double bal_val) : name( name_val ), balance( bal_val ){}
That should be the correct definition of your class constructors.
Account empty;
Account frank("Frank" );
Account rich_boi( 1000000.0 );
Account sad_boi( "Sad boi", -10.0 );
And that's how you should declare your objects in the main()
of your program. You syntax was incorrect.
The correct syntax of initialisation list in C++ is :
Constructorname(datatype value1, datatype value2):
datamember(value1),
datamember(value2) {
.
. //Anything if at all you want to include in your constructor function
.
}
Upvotes: 1