Saturday, December 15, 2012

A program for understanding 'Array of pointer'

#include <iostream>
#include <string.h>
using namespace std;

class city
{
    protected:
        int len;
        char*name;
       
    public:
        city()
        {
            len = 0;
            name = new char[len + 1];
        }
       
        void getname()
        {
            char *a;
            a = new char[30];
       
            cout << "\n Enter city name: ";
            cin >> a;
            len = strlen(a);
            name = new char[len + 1];
            strcpy(name, a);
        }
   
        void putname()
        {
            cout << "\n city name: " << name;
        }
};

int main()
{
    city *p[10];
    int n = 1;
    int opt;
   
    do
    {
        p[n] = new city;
        p[n++] -> getname();
        cout << "\n enter 1 for more entry of city name or 0 for exit: ";
        cin >> opt;
    }while(opt);
   
    cout << "\n\n";
    for (int i=1; i<=n; i++)

    {
        p[i] -> putname();

    }
}

No comments:

Post a Comment