Object Oriented Programming
3341602
 practicals 1 to 17
Practical 1:-Develop programs using Input / Output operators.
#include <iostream> 
using namespace std; 
int main() 
{ 
    int age; 
    cout << "Enter your age:"; 
    cin >> age; 
    cout << "\nYour age is: " << age; 
    return 0; 
} 
Practical 2:- Develop programs using Control structure.
#include <iostream>
using namespace std;
int main ()
{
  int n;
  cout << "Enter the starting number > ";
  cin >> n;
 for(i = 1 ; i < = n ; i = i + 2 )
{
cout<<i<<endl;
}
  return 0;
}
Practical 3:-Develop programs using array of objects
#include <iostream>   
class MyClass { 
  int x; 
public: 
  void setX(int i) { x = i; } 
  int getX() { return x; } 
}; 
 void main() 
{ 
  MyClass obs[4]; 
  int i; 
  for(i=0; i < 4; i++) 
    obs[i].setX(i); 
  for(i=0; i < 4; i++) 
    cout << "obs[" << i << "].getX(): " << obs[i].getX() << "\n"; 
  getch(); 
}
Practical 4:-Develop programs using call by value ,call by reference and function overloading
call bye value:--
#include <iostream>using namespace std;void swaping(int x, int y);int main () {   int a = 100, b = 200;   cout << "Before swap, value of a :" << a << endl;   cout << "Before swap, value of b :" << b << endl;    swaping(a, b);    cout << "After swap, value of a :" << a << endl;   cout << "After swap, value of b :" << b << endl;    return 0;}
#include <iostream>
using namespace std;
void swaping(int x, int y);
int main () {
   int a = 100, b = 200;
   cout << "Before swap, value of a :" << a << endl;
   cout << "Before swap, value of b :" << b << endl;
    swaping(a, b);
   cout << "After swap, value of a :" << a << endl;
   cout << "After swap, value of b :" << b << endl;
   return 0;
}
call bye reference:--
#include <iostream>  using namespace std;  void change(int data);  int main()  {  int data = 3;  change(data);  cout << "Value of the data is: " << data<< endl;  return 0;  }  void change(int data)  {  data = 5;  }  
#include <iostream>  
using namespace std;  
void change(int data);  
int main()  
{  
int data = 3;  
change(data);  
cout << "Value of the data is: " << data<< endl;  
return 0;  
}  
void change(int data)  
{  
data = 5;  
}  
Function overloading:--
#include <iostream>  using namespace std;  void change(int data);  int main()  {  int data = 3;  change(data);  cout << "Value of the data is: " << data<< endl;  return 0;  }  void change(int data)  {  data = 5;  }  
#include <iostream>  
using namespace std;  
void change(int data);  
int main()  
{  
int data = 3;  
change(data);  
cout << "Value of the data is: " << data<< endl;  
return 0;  
}  
void change(int data)  
{  
data = 5;  
}  
Practical 5:-Develop programs on default arguments, constant arguments
default argument:--
#include<iostream> using namespace std; int sum(int x, int y, int z=0, int w=0) { return (x + y + z + w); } int main() { cout << sum(10, 15) << endl; cout << sum(10, 15, 25) << endl; cout << sum(10, 15, 25, 30) << endl; return 0; }
#include<iostream> 
using namespace std; 
int sum(int x, int y, int z=0, int w=0) 
{ 
return (x + y + z + w); 
} 
int main() 
{ 
cout << sum(10, 15) << endl; 
cout << sum(10, 15, 25) << endl; 
cout << sum(10, 15, 25, 30) << endl; 
return 0; 
}
constant argument:--
#include<iostream> using namespace std; int main(){int i , pi=3.14,area;
cout<<"enter radius of circle";cin>>i;
area = i*i*pi;
cout<<"area of circle is "<<area;
return 0; }
#include<iostream> 
using namespace std; 
int main()
{
int i , pi=3.14,area;
cout<<"enter radius of circle";
cin>>i;
area = i*i*pi;
cout<<"area of circle is "<<area;
return 0; 
}
Practical 6:-Develop programs on function overloading
#include<iostream>
using namespace std;
inline int add(int a,int b);
class A
{
    public:
      A(int a,int b)
      {
        cout<<"a + b is ="<<a<<endl;
      }
~A(){
}
};
int main()
{
A obj(2,3);
inline int add(int a,int b);
return 0;
}
Practical 7:-Develop programs using different classes such as student, distance, shape, employee, feet, time, data etc. with data member & member function
#include <iostream>#include <stdio.h>using namespace std; //Base Class - basicInfoclass basicInfo{    protected:          char    name[30];        int     empId;        char    gender;    public:        void getBasicInfo(void)        {            cout << "Enter Name: ";             cin>>name;            cout << "Enter Emp. Id: ";            cin  >> empId;            cout << "Enter Gender: ";            cin  >> gender;        }};  
class employee:private basicInfo{    public:        void getEmployeeInfo(void){            cout << "Enter employee's basic info: " << endl;            //call getBasicInfo() of class basicInfo            getBasicInfo();     //calling of public member function            cout << "Enter employee's department info: " << endl;                   }        void printEmployeeInfo(void)        {            cout << "Employee's Information is: "     << endl;            cout << "Basic Information...:"       << endl;            cout << "Name: " << name   << endl;                cout << "Employee ID: "<< empId  << endl;                   cout << "Gender: "<< gender << endl << endl;                                 }}; int main(){
    employee emp;         emp.getEmployeeInfo();    emp.printEmployeeInfo();         return 0;}
#include <iostream>
#include <stdio.h>
using namespace std;
//Base Class - basicInfo
class basicInfo
{
    protected:  
        char    name[30];
        int     empId;
        char    gender;
    public:
        void getBasicInfo(void)
        {
            cout << "Enter Name: "; 
            cin>>name;
            cout << "Enter Emp. Id: ";
            cin  >> empId;
            cout << "Enter Gender: ";
            cin  >> gender;
        }
};
class employee:private basicInfo
{
    public:
        void getEmployeeInfo(void){
            cout << "Enter employee's basic info: " << endl;
            //call getBasicInfo() of class basicInfo
            getBasicInfo();     //calling of public member function
            cout << "Enter employee's department info: " << endl;
        }
        void printEmployeeInfo(void)
        {
            cout << "Employee's Information is: "     << endl;
            cout << "Basic Information...:"       << endl;
            cout << "Name: " << name   << endl;    
            cout << "Employee ID: "<< empId  << endl;       
            cout << "Gender: "<< gender << endl << endl;
        }
};
int main()
{
    employee emp;
    emp.getEmployeeInfo();
    emp.printEmployeeInfo();
    return 0;
}
Practical 8:-Develop Programs using array of objects and static member functions
#include <iostream> 
using namespace std; 
class test 
{ 
int objNo; 
static int objCnt; 
public: 
    test() 
    { 
    objNo = ++objCnt; 
    } 
    ~test() 
    { 
    --objCnt; 
    } 
    void printObjNumber(void) 
    { 
    cout << "object number :" << objNo << "\n"; 
    } 
    static void printObjCount(void) 
    {
    cout << "count:" << objCnt<< "\n"; 
    } 
}; 
int test::objCnt; 
int main() 
{ 
test t1, t2; 
test::printObjCount(); 
test t3; 
test::printObjCount(); 
t1.printObjNumber(); 
t2.printObjNumber(); 
t3.printObjNumber(); 
return 0; 
} 
Practical 9:-Develop program using friend function.
#include<iostream>
using namespace std;
class complex
{
    int b,c;
    public:
    friend void show(complex ma);
};
void show(complex ma)
{cout<<"enter b";
    cin>>b;
    cout<<"enter c";
    cin>>c;
    ma.b=20;
    ma.c=30;
    cout<<"b is "<<ma.b+ma.c;
};
int main()
{
complex ma;
show(ma);
return 0;
}
Practical 10:-Develop programs using various types of constructors and destructor.
default constructor:--
#include<iostream>
using namespace std;
class b
{
    int a;
    public:
        b()
       {
        a=10;
         cout<<"no is :"<<a;
    }
};
int main()
{
b ob;
return 0;
}
copy constructor:--
#include<iostream>
using namespace std;
class complex
{
int a;
public:
    complex(int b)
    {
        a=b;
    }
    complex(complex &obj)
    {
        a=obj.a;
    }
    void verify()
    {
    cout<<"the is blah blah blah ="<<a; 
    }
};
int main()
{
complex ob(12);
complex ob1(ob);
ob.verify();
ob1.verify();
return 0;
}
parameterized constructor:--
#include<iostream>
using namespace std;
class b
{
    int a;
    public:
        b()
       {
        a=10;
         cout<<"no is :"<<a;
    }
};
int main()
{
b ob;
return 0;
}
destructor:--
#include<iostream>
using namespace std;
class Demo {
   private:
   int num1, num2;
   public:
   Demo(int n1, int n2) {
      cout<<"Inside Constructor"<<endl;
      num1 = n1;
      num2 = n2;
   }
   void display() {
      cout<<"num1 = "<< num1 <<endl;
      cout<<"num2 = "<< num2 <<endl;
   }
   ~Demo() {
      cout<<"Inside Destructor";
   }
};
int main() {
   Demo obj1(10, 20);
   obj1.display();
   return 0;
}
Practical 11:-Develop programs using single, multilevel, multiple Inheritance
Multilevel Inheritance:--
#include<iostream>
using namespace std;
class A
{
    protected:
        int a=12;
};
class B:public A
{
    protected:
        int b=13;
};
class C:public B
{
private:
int c=15;
public:
void data()
{
    cout<<a<<b<<c;
}
};
int main()
{
    C obj;
    obj.data();
return 0;
}
Multiple Inheritance:--
#include<iostream>
using namespace std;
class A
{
    protected:
        int a=12;
        
};
class B
{
    protected:
        int b=13;
};
class C: public B,public A
{
private:
int c=a+b;
public:
void data()
{
    cout<<c;
}
};
int main()
{
    C obj;
    obj.data();
return 0;
}
Single Inheritance:--
#include<iostream>
using namespace std;
class A
{
    protected:
        int a;
};
class B:public A
{
private:
a=15;
public:
void data()
{
    cout<<a;
}
};
int main()
{
    B obj;
    obj.data();
return 0;
}
Practical 12:- Develop programs using inheritance and constructors.
#include <iostream>
using namespace std;
class Base
{
   int x;
public:
   // default constructor
   Base()
   {
      cout << "Base default constructor\n";
   }
};
class Derived : public Base
{
   int y;
public:
   // default constructor
   Derived()
   {
      cout << "Derived default constructor\n";
   }
   // parameterized constructor
   Derived(int i)
   {
      cout << "Derived parameterized constructor\n";
   }
};
int main()
{
   Base b;
   Derived d1;
   Derived d2(10);
   return 0;
}
Practical 13:- Develop programs using Virtual base class.
#include<iostream>
using namespace std;
class A {
   public:
   int a;
   A(){
      a = 10;
   }
};
class B : public virtual A {
};
class C : public virtual A {
};
class D : public B, public C {
};
int main(){
   //creating class D object
   D object;
   cout << "a = " << object.a << endl;
   return 0;
}
Practical 14:- Develop programs using ‘this’ key word.
#include <iostream>
using namespace std;
class Demo {
private:
  int num;
  char ch;
public:
  void setMyValues(int num, char ch){
    this->num =num;
    this->ch=ch;
  }
  void displayMyValues(){
    cout<<num<<endl;
    cout<<ch;
  }
};
int main(){
  Demo obj;
  obj.setMyValues(100, 'A');
  obj.displayMyValues();
  return 0;
}
Practical 15 :- Develop programs using virtual function.
#include <iostream>  
using namespace std;  
class Demo  
{  
    int a=10;  
    public:  
    void show()  
    {  
        std::cout << "Disp the value of a = " <<a<<std::endl;  
    }  
};  
class Demo1: public Demo  
{  
    int b = 15;  
    public:  
    void show()  
    {  
        std::cout <<"Disp the Value of b = "<<b<<std::endl;  
    }  
};  
int main()  
{  
    Demo *obj;  
    Demo1 obj1;  
    obj = &obj1;  
    obj->show();  
    return 0;  
}  
Practical 16:- Develop programs using unformatted input/output functions
#include<iostream8>
using namespace std;
int main()
{
char country[20];
cout<<"Enter a country you want to visit : ";
cin.getline(country,20);
cout<<"The country you want to visit is : " << country;
return 0;
}
Practical 17:-Develop programs using formatted input/output functions.
#include <iostream>
using namespace std;
int main()
{
char ch = 'a';
cout.width(5);
cout<<ch <<"\n";
int i = 1;
cout<<i;
}
0 Comments