124697
124697

Reputation: 21901

How do you create an instance of a class in C++

I'm a C++ beginner. I have a class and when I try to compile it says it's missing 'main'.

What do I need to do to create an instance of this class and access its methods?

#include <string>
#include <iostream>

using namespace std;

class Account
{
      string name;
      double balance;

  public:
      Account(string n);
      Account(string n, double initial_balance);

      string get_name() const;
      double get_balance() const;

      void deposit(double amount);

      void widthdraw(double amount);

};

edit:

Where do I put the main method?

I have tried putting it in a different file like the following

    #include <Account>
    int main(){
    Account:: Account(string n) : name(n), balance(0){}
    return 0 ;
    }

but this gives an error saying there is not Account in the directory. I'm guessing this is because it's not compiled

edit:

Both files are in the same directory

account_main.cc

    #include<string>
    #include <iostream>
    #include "Account.cc"

    Account:: Account(string n) : name(n), balance(0){} 
    int main()
    {
        Account account("Account Name"); // A variable called "account"
        account.deposit(100.00); // Calls the deposit() function on account

        return 0 ;
    }

Account.cc

#include<string>
#include <iostream>

using namespace std;

class Account
{
      string name;
      double balance;

  public:
      Account(string n);
      Account(string n, double initial_balance);

      string get_name() const;
      double get_balance() const;

      void deposit(double amount);

      void widthdraw(double amount);

};

Upvotes: 0

Views: 4241

Answers (3)

Roman Kutlak
Roman Kutlak

Reputation: 2784

as has been pointed out, you need a decent book. To answer your questions you should know the following: Classes are usually defined in .h file and implemented in .cpp or .cc file. You should have three files: Account.h, Account.cc and main.cc. You only compile the .cc files and the .h files are included in the parts of code where you need to know something about the class (i.e. when you are actually doing something with the class). If you are using g++ (linux, unix or mingw I think) you can compile the program using the following command: g++ main.cc Account.cc -o program_name

main.cc:

#include <iostream>
using namespace std;

#include "Account.h"
int main() {
  Account a("first account");

  cout << a.get_balance() << endl;
  a.deposit(100.0);
  cout << a.get_balance() << endl;

  return 0;
}

Your Account.h file should look like this:

#include<string>
//#include <iostream> -- not needed here

// using namespace std; -- don't use 'using' in header files

class Account
{
  std::string name;
  double balance;

public:
    Account(std::string n);
    Account(std::string n, double initial_balance);

    std::string get_name() const;
    double get_balance() const;

    void deposit(double amount);

    void widthdraw(double amount);

};

And finally your Account.cc file should implement the class.

#include "Account.h"

using namespace std;

Account::Account(string n) : name(n), balance(0.0) {}

Account::Account(string n, double initial_balance) :
name(n), balance(initial_balance) {}

string Account::get_name() const {
  return name;
}

double Account::get_balance() const {
  return balance;
}

void Account::deposit(double amount) {
  balance += amount;
}

void Account::widthdraw(double amount) {
  balance -= amount; // generously allow overdraft...
}

Hope that helps.

Roman

Upvotes: 1

In silico
In silico

Reputation: 52217

All C++ programs require what's called an entry point. The main() function is always the entry point for standard C++ programs. You need to provide a main(), function otherwise the linker will complain. You can write a main() function in one of two ways:

int main()
{
    return 0;
}

Or, if you are expecting command-line arguments:

int main(int argc, char ** argv)
{
    return 0;
}

Note that void main() is not valid in C++. Also note that a return statement isn't strictly necessary for main() functions, but you should explicitly write one anyway, for the sake of consistency.

C++ Standard 3.6.1 Main function [basic.start.main]

5. A return statement in main has the effect of leaving the main function (destroying any objects with automatic storage duration) and calling exit with the return value as the argument. If control reaches the end of main without encountering a return statement, the effect is that of executing

    return 0;

To answer your question about your latest edit:

#include <Account>

int main(){ 
    Account:: Account(string n) : name(n), balance(0){} 
    return 0 ; 
} 

The form of main() is correct, but this is not how you provide class member definitions. The constructor needs to be outside the main function.

#include <Account>

// Outside of main()
Account::Account(string n) : name(n), balance(0)
{
} 

int main()
{ 
     return 0 ; 
} 

To create an instance of Account, you declare a variable and pass all the required constructor arguments like this:

int main()
{
    Account account("Account Name"); // A variable called "account"
    account.deposit(100.00); // Calls the deposit() function on account
                             // Make sure you provide a function
                             // definition for Account::deposit().
    return 0;
}

Also, check the exact file path of where class Account is. If the Account class is in a file called Account.h and is in the same folder as the file containing the main() function, then you need to use #include "Account.h" instead of #include <Account> in that file. For example:

#include "Account.h" // Note .h and quotes

Account::Account(string n) : name(n), balance(0)
{
} 

int main()
{
    // ...
    return 0;
}

This is actually a rather fundamental aspect of the C++ programming language. Surely you have a book that covers this? In fact, main() functions and #include statements are usually the first thing that you learn when programming in C++. I highly recommend that you pick up a good C++ book and read through them and do the exercises.

Upvotes: 8

Mooing Duck
Mooing Duck

Reputation: 66981

For your latest edit: not Account in the directory

try this:

#include "Account.h"  //quotes, and .h


Account:: Account(string n)  //this must be outside of main, it's its own function
: name(n), balance(0) //I put these on three seperate lines but
{}                    //that's a personal choice

int main(){  //this is what the code should do when it starts
    Account person1("George"); //create a Account called person1 with the name George
    cout << person1.get_name(); //c-output person1's name.
    return 0 ; //return success (success=0)
}

Upvotes: 1

Related Questions