Program to illustrate Parameterised Constructor


#include <iostream.h>
#include <string.h>
#include<conio.h>
class sample
{
        char itemname[20];
        int cost;
        public :
        sample(char*, int);        // Constructor declaration
        void print_data();
};
sample::sample(char* s, int c)    // Constructor definition
{
    strcpy(itemname,s);
    cost = c;
}
void sample::print_data()
{
    cout << "Item : " << itemname << "\n";
    cout << "Cost : " << cost << "\n";
}

void main(void)
{
    sample A1("Pencil", 5);            // implicit call to the constructor
    sample A2 = sample("Notebook", 20);    // explicit call to the constructor
    clrscr();
    A1.print_data();
    A2.print_data();
    getch();
}

1 comments:

Anonymous said...

I love your blog.. very nice colors & theme.
Did you create this website yourself or did you hire someone to
do it for you? Plz answer back as I'm looking to construct my own blog and would like to find out where u got this from. kudos

My blog; natural cellulite treatment

Post a Comment

 
 
 
 


Copyright © 2012 http://codeprecisely.blogspot.com. All rights reserved |Term of Use and Policies|