Use vector to store the calsses in a hiearchy : object vector « vector « C++ Tutorial

Home
C++ Tutorial
1.Language Basics
2.Data Types
3.Operators statements
4.Array
5.Development
6.Exceptions
7.Function
8.Structure
9.Class
10.Operator Overloading
11.Pointer
12.File Stream
13.template
14.STL Introduction
15.string
16.vector
17.list
18.bitset
19.set multiset
20.valarray
21.queue stack
22.deque
23.map multimap
24.STL Algorithms Modifying sequence operations
25.STL Algorithms Non modifying sequence operations
26.STL Algorithms Binary search
27.STL Algorithms Sorting
28.STL Algorithms Merge
29.STL Algorithms Min Max
30.STL Algorithms Iterator
31.STL Algorithms Heap
32.STL Algorithms Helper
C / ANSI-C
C Tutorial
C++
Visual C++ .NET
C++ Tutorial » vector » object vector 
16.31.6.Use vector to store the calsses in a hiearchy
#include <iostream>
#include <string>
#include <vector>
using namespace std;

class Person {                            
    string firstName, lastName;
public:
    Personstring fnam, string lnam ): firstNamefnam ), lastNamelnam ) {}
    virtual void print() const cout << firstName << " " << lastName << " "}
    virtual ~Person(){}                                               
};

class Employee : public Person {
    string companyName;
public:
    Employeestring fnam, string lnam, string cnam )
        : Personfnam, lnam ), companyNamecnam ) {}
    void print() const {
        Person::print();
        cout << companyName << " ";
    }
    ~Employee(){}                                                     
};

class Manager : public Employee {                 
    short level;
public:
    Managerstring fnam, string lnam, string cnam, short lvl )
        : Employeefnam, lnam, cnam ), levellvl ) {}

    void print() const {
      Employee::print();
      cout << level;
    }
    ~Manager(){}                                  
};
int main()
{
    vector<Employee*> empList;

    Employee* e1 = new Employee"A""B""C" );
    Employee* e2 = new Employee"D""E""F" );
    Employee* m3 = new Manager("G""H""I" 2);
    Employee* m4 = new Manager("J""J""L"2);

    empList.push_backe1 );
    empList.push_backe2 );
    empList.push_backm3 );
    empList.push_backm4 );

    vector<Employee*>::iterator p = empList.begin();
    while p < empList. end() ) {                
        (*p++)->print();                          
        cout << endl;
    }

    delete e1;
    delete e2;
    delete m3;
    delete m4;

    return 0;
}
16.31.object vector
16.31.1.Store a class object in a vector.
16.31.2.vector with 10 objects
16.31.3.Object vector
16.31.4.Call member function for each element in vector
16.31.5.Call the virtual function from classes stored in a vector
16.31.6.Use vector to store the calsses in a hiearchy
16.31.7.Use vector as an array for user-defined object
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.