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