// VitrualFunction070720120023.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
usingnamespace std;
class Person
{
private:
int a ;
virtualvoid display() {}
public:
Person() { }
void m_display() { display() ; }
virtual ~Person() { }
};
class Student : public Person
{
private:
int b ;
public:
Student() { }
~Student() { }
void display() { cout <<"\n This is the display for the student "; }
};
class Professional : public Person
{
private:
int c;
public:
Professional() { }
~Professional() { }
void display() { cout<<"\n We are the professional "; }
};
//deriver function
void Test( Person* per )
{
per->m_display();
}
//Main function
int _tmain(int argc, _TCHAR* argv[])
{
Person[] person = { new Student() , new Professional() };
for( int i = 0 ; i < 2 ; i++)
{
Test( person[i] ) ;
}
for( int i = 0 ; i < 2 ; i++)
{
delete person[i] ;
}
return 0;
}
giving the error as
projects\vitrualfunction070720120023\vitrualfunction070720120023\vitrualfunction070720120023.cpp(45) : error C2143: syntax error : missing ';' before '['
1>j:\projects\vitrualfunction070720120023\vitrualfunction070720120023\vitrualfunction070720120023.cpp(45) : error C3409: empty attribute block is not allowed
1>j:\projects\vitrualfunction070720120023\vitrualfunction070720120023\vitrualfunction070720120023.cpp(45) : error C2146: syntax error : missing ';' before identifier 'person'
1>j:\projects\vitrualfunction070720120023\vitrualfunction070720120023\vitrualfunction070720120023.cpp(45) : error C2065: 'person' : undeclared identifier
1>j:\projects\vitrualfunction070720120023\vitrualfunction070720120023\vitrualfunction070720120023.cpp(45) : error C2059: syntax error : '{'
1>j:\projects\vitrualfunction070720120023\vitrualfunction070720120023\vitrualfunction070720120023.cpp(45) : error C2143: syntax error : missing ';' before '{'
1>j:\projects\vitrualfunction070720120023\vitrualfunction070720120023\vitrualfunction070720120023.cpp(45) : error C2143: syntax error : missing ';' before '}'
1>Build log was saved at "file://j:\Projects\VitrualFunction070720120023\VitrualFunction070720120023\Debug\BuildLog.htm"
Question is how do we create the array of the object dynamically .
please throw some light on it .. i have been surfing on net but not able to get the concept .
thanks in advance .
Looks like you are trying to mix in some C# there.
The proper way to do it is like this:
1 2 3 4 5 6 7 8
Person* person[2] = { new Student(), new Professional() };
// ..
for( int i = 0 ; i < 2 ; i++)
{
delete person[i] ;
}
Or... if you also want the size of the array to be dynamic....
1 2 3 4 5 6 7 8 9 10 11 12
int x = /*number of people*/
Person** person = new Person[x];
for(int i = 0; i < x; ++i)
{
person[i] = new Whatever();
}
//..
for(int i = 0; i < x; ++i)
delete person[i];
delete[] person;
EDIT:
or... better yet ... use smart pointers and containers:
1 2 3 4 5 6 7
typedef std::shared_ptr<Person> PersonPtr;
std::vector< PersonPtr > people;
people.push_back( PersonPtr( new Student() ) );
people.push_back( PersonPtr( new Professional() ) );
// all cleanup is done automatically. No need to delete anything