instantiation in c++

First of all, I would like to notify you, I have definetly searched for answers about my following question, but im a complete newbie to C++.

I am coming from the luxurious life of C# and Java, now trying to pick up a thing or two about c++

The question is about instantiation.

I use code::block as my IDE of choice.

Currently I am just playing around with what in C# (which I'm actually quite familiar with and have written several applications in) is

2 classes

the class containing main and Person

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


using Models.Person;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
           Person person = new Person();
           Console.WriteLine(person.getName());
        }
    }
}

and the person class:

namespace ConsoleApplication1
{
   public class Person{
       private string name = "Bob";

       public string getName(){
           return name;
       }
   }
}

(dont mind bad or right written syntax, its just to simulate what i want to achieve)

I want to achieve the same in C++

I have looked up, and learned about headers to some extent, and picked up a bit of the syntax. This is what I have right now.

main.cpp

#include <iostream>
using namespace std;

int main()
{
   Person person;
   cout << person->getName() << endl;
}

Person.h

#ifndef PERSON_H
#define PERSON_H

#include <string>

class Person
{
    public:
        Person();
        virtual ~Person();
        std::string getName();
    protected:
    private:
};

#endif // PERSON_H

player.cpp

#include "Person.h"
#include <string>

using std::string;

Person::Person()
{
    //ctor
}

Person::~Person()
{
}

string Person::getName()
{
    return name;
}

Considering the above code, I have multiple questions.

I never found a good source that showed me whether I should instantiate using Person person; or Person person = new Person();

Which one of the two is the one I'm supposed to use?

And another question I'm dying for is, do I define class members in the header file or in the class file?

Currently I'm recieving the following errors:

'person' was not declared in scope and expected ';' before person

I'm not nescesarily asking you to resolve my errors, I'll manage that upon recieving answers on my questions.

5
задан Joey Roosing 21 March 2011 в 15:44
поделиться