161. Write a program to initialize member variable by using constructor?

#include<iostream.h> #include<conio.h> class demo {                 int x; public:                 demo()                                 {                                 x=20;                                 }                 void display()                                 {                                 cout<<"X = "<<x<<endl;                                 } }; void main()                 {                 demo d;                 clrscr();                 d.display();                 getch(); }

Advertisements

160. Write a program to define a constructor explicitly behalf of compiler?

#include<iostream.h> #include<conio.h> class demo {                 int x; public:                 demo()                                 {                                 }                 void read()                                 {                                 x=10;                                 }                 void display()                                 {                                 cout<<"X = "<<x<<endl;                                 } }; void main()                 {                 demo d;                 clrscr();                 d.read();                 d.display();                 getch(); }

Create a website or blog at WordPress.com

Up ↑

%d bloggers like this: