C++ program to print the addition of numbers using default constructor
kw.cpp
#include<iostream>usingnamespacestd;classkw{inta,b;public:voidadd(){cout<<"The addition of "<<a<<"+"<<b<<"="<<(a+b)<<"\n";}kw(){a=10;b=10;}};intmain(){kwk;k.add();return0;}
Output
kodingwindow@kw:~$ g++ kw.cpp kodingwindow@kw:~$ ./a.out
The addition of 10+10=20
kodingwindow@kw:~$
C++ program to print the addition of numbers using parameterized constructor
kw.cpp
#include<iostream>usingnamespacestd;classkw{floata,b;public:voidadd(){cout<<"The addition of "<<a<<"+"<<b<<"="<<(a+b)<<"\n";}kw(){a=10,b=10;}kw(intx,floaty){a=x;b=y;}};intmain(){kwk,d(10,10.02351);k.add();d.add();return0;}
Output
kodingwindow@kw:~$ g++ kw.cpp kodingwindow@kw:~$ ./a.out
The addition of 10+10=20
The addition of 10+10.0235=20.0235
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.