netx_iit

How to create class and object in C++

Jun 27th, 2020 (edited)
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.65 KB | None | 0 0
  1. /* Create demo class and two member function 1st one is getting data and second one is display data*/
  2. #include<iostream.h>
  3. #include<stdio.h>
  4. #include<conio.h>
  5. class demo
  6. {
  7.     //public:
  8.     int age;
  9.     char name[20];//data member;
  10.     public:   //private,protected,public (by def private)
  11.     void getData()//member function
  12.     {
  13.         cout<<"\nEnter your name:";
  14.         cin>>name;
  15.         cout<<"\nEnter your age:";
  16.         cin>>age;
  17.     }
  18.  
  19.     void display()//member function
  20.     {
  21.         cout<<"\nYour name is "<<name<<" and age is "<<age;
  22.     }
  23. };
  24. int main()
  25. {
  26.     demo d1;//d1 is object
  27.     clrscr();
  28.     cout<<"\n\n";
  29.     cout<<d1.age;
  30.     d1.getData();
  31.     d1.display();
  32.     getch();
  33.     return 0;
  34. }
Add Comment
Please, Sign In to add comment