Example 3: C++ program to demonstrate the use of class and object
gd.cpp
#include<iostream>usingnamespacestd;classgd{public:voidadd();};voidgd::add(){cout<<"The addition of 10+10="<<10+10<<"\n";}intmain(){gdk;k.add();return0;}
Output
godarda@gd:~$ g++ gd.cpp godarda@gd:~$ ./a.out
The addition of 10+10=20
godarda@gd:~$
Example 4: C++ program to demonstrate the use of class and object
gd.cpp
#include<iostream>usingnamespacestd;classgd{private:inta,b;public:voidgetdata();voidadd();};voidgd::getdata(){cout<<"Enter the 1st number ";cin>>a;cout<<"\nEnter the 2nd number ";cin>>b;}voidgd::add(){if(b<0){cout<<"\nThe addition of "<<a<<b<<" = "<<(a+b)<<"\n";}else{cout<<"\nThe addition of "<<a<<"+"<<b<<" = "<<(a+b)<<"\n";}}intmain(){gdk;k.getdata();k.add();return0;}
Output
godarda@gd:~$ g++ gd.cpp godarda@gd:~$ ./a.out
Enter the 1st number 10
Enter the 2nd number -50
The addition of 10-50 = -40
godarda@gd:~$
Dear User, Thank you for visitng GoDarda. If you are interested in technical articles, latest technologies, and our journey further, please follow us on LinkedIn.