#include<iostream> int main() { int a,b,c; std::cout<<"Enter The Value of A B:"; std::cin>>a>>b; c=a+b; std::cout<<"Total : "<<c; return 0; }
Enter The Value of A B: 25 35 Total : 60
#include<iostream> using namespace std; class basic { public: string name; void display() { cout<<"Welcome "<<name<<endl; } }; int main() { basic o1; o1.name="Ram"; o1.display(); return 0; }
Welcome Ram
#include<iostream> using namespace std; class basic { public: string name; void display() { cout<<"Welcome "<<name<<endl; } }; int main() { basic *o1 =new basic(); o1->name="Ram"; o1->display(); return 0; } /* Normal Obj store in stack memory its static. Pointer Obj store in Heap Memory its Dynamic. */
Welcome Ram
#include<iostream> using namespace std; class Maths { public : int a,b,c; void add() { cout<<"Enter The Value Of A and B :"<<endl; cin>>a>>b; c=a+b; cout<<"\nTotal : "<<c<<endl; } void sub(); }; void Maths::sub() { cout<<"Enter The Value Of A and B :"<<endl; cin>>a>>b; c=a-b; cout<<"Difference : "<<c; } int main() { Maths o; o.add(); o.sub(); return 0; }
Enter The Value Of A and B : 100 25 Total : 125 Enter The Value Of A and B : 100 25 Difference : 75
#include<iostream> using namespace std; class Users { private: string name; public: string getName() { return name; } void setName(string n) { name=n; } }; int main() { Users o; o.setName("Ram Kumar"); cout<<"Name : "<<o.getName(); return 0; }
Name : Ram Kumar
#include<iostream> using namespace std; class basic { /* Constructor is Special Function. Called When Object is Created. */ private : int a,b,c; public: basic(){ cout<<"Constructor Called"<<endl; a=10; b=20; } void add() { c=a+b; cout<<"Total : "<<c<<endl; } }; int main() { basic o1; o1.add(); return 0; }
Constructor Called Total : 30
#include<iostream> using namespace std; class basic { private : int a,b,c; public: basic(int x,int y){ a=x; b=y; } void add() { c=a+b; cout<<"Total : "<<c<<endl; } }; int main() { basic o1(10,35); o1.add(); return 0; }
Total: 45
#include<iostream> using namespace std; class Rectangle { private: int length; int breadth; public : Rectangle(int l,int b) { length=l; breadth=b ; } Rectangle(Rectangle &o1) { length=o1.length; breadth=o1.breadth; } int area() { return length*breadth; } }; int main() { Rectangle o1(10,5),o2(o1); cout<<"\nArea : "<<o1.area(); cout<<"\nArea : "<<o2.area(); return 0; }
Area : 50 Area : 50
#include<iostream> using namespace std; class user { private : string name; int age; public: user(){ name="No Name"; age=0; } user(string n){ name=n; age=0; } user(string n,int a){ name=n; age=a; } void display() { cout<<"Name : "<<name<<endl<<"Age : "<<age<<endl; } }; int main() { /* user o; o.display(); user o("Ram"); o.display(); */ user o("Ram",25); o.display(); return 0; }
Name : Ram Age : 25
#include<iostream> using namespace std; class user { private : string name; int age; public: user(){ name="No Name"; age=0; } user(string n,int a=0){ name=n; age=a; } void display() { cout<<"Name : "<<name<<endl<<"Age : "<<age<<endl; } }; int main() { user o1; o1.display(); user o2("Ram"); o2.display(); user o3("Ram",25); o3.display(); return 0; }
Name : No Name Age : 0 Name : Ram Age : 0 Name : Ram Age : 25
/* Destructors is Special Function. Called When Object is goes out of Scope. */ #include<iostream> using namespace std; class user { public: user() { cout<<"Constructor is Called Object Created"<<endl; } ~user() { cout<<"Destructors is Called Object Destroyed"<<endl; } }; int main() { user o; return 0; }
Constructor is Called Object Created Destructors is Called Object Destroyed
#include<iostream> using namespace std; class user { public: user() { cout<<"Constructor is Called Object Created"<<endl; } ~user() { cout<<"Destructors is Called Object Destroyed"<<endl; } }; int main() { user *o=new user(); delete o; return 0; }
Constructor is Called Object Created Destructors is Called Object Destroyed
#include<iostream> using namespace std; void print() { static int n=0; cout<<"Count is : "<<++n<<endl; } int main() { print(); print(); print(); return 0; }
Count is : 1 Count is : 2 Count is : 3
#include<iostream> using namespace std; class user { public: static int count; user() { count++; } }; int user::count=0; int main() { user ram; user sam; cout<<"Total Users : "<<user::count; return 0; }
Total Users : 2
#include<iostream> //Friend Function is a Specific Function to Access the Private data of a Class using namespace std; class user { private: string name; int age; public: friend void display(user a); user(string n,int a) { name=n; age=a; } }; void display(user a) { cout<<"Name : "<<a.name<<endl<<"Age : "<<a.age<<endl; } int main() { user o("Ram",25); display(o); return 0; }
Name : Ram Age : 25
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions