Object Oriented Programming in C++ Tamil



Basic Program in C++ Programmming

#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;
}

Output

Enter The Value of A B:
25
35
Total : 60

Class in C++ Programmming

#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;
}

Output

Welcome Ram

Pointer Object Class in C++ Programmming

#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.
*/

Output

Welcome Ram

Scope Resolution Operator in C++ Programmming

#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;
}

Output

Enter The Value Of A and B :
100
25

Total : 125

Enter The Value Of A and B :
100
25
Difference : 75

Access Specifiers in C++ Programmming

#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;
}

Output

Name  : Ram Kumar

Constructor in C++ Programmming

#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;
}

Output

Constructor Called
Total : 30

Parameterized Constructor in C++ Programmming

#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;
}

Output

Total: 45

Copy Constructor in C++ Programmming

#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;
}

Output

Area : 50
Area : 50

Constructor Overloading in C++ Programmming

#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;
}

Output

Name : Ram
Age  : 25

Default Value Constructor in C++ Programmming

#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;
}

Output

Name : No Name
Age : 0
Name : Ram
Age : 0
Name : Ram
Age : 25

Destructor in C++ Programmming

/*
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;
}

Output

Constructor is Called Object Created
Destructors is Called Object Destroyed

Destructor Using Pointer in C++ Programmming

#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;
}

Output

Constructor is Called Object Created
Destructors is Called Object Destroyed

Static Variables in C++ Programmming

#include<iostream>
using namespace std;
void print()
{
    static int n=0;
    cout<<"Count is : "<<++n<<endl;
}
int main()
{
    print();
    print();
    print();
    return 0;
}

Output

Count is : 1
Count is : 2
Count is : 3

Static Variables in Class C++ Programmming

#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;
}

Output

Total Users : 2

Friend Function C++ Programmming

#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;
}

Output

Name  : Ram
Age : 25

Live Stream Class