Practical Programs Solutions


Practical Programs





1. WAP for students details in c++


#include<iostream>
using namespace std;
class students
{
private:

char name[20];
int rollno;
char branch[10];
int sems;
public:
void input();
void display();


};
void students::input()
{
cout<<"Enter Your Name:";
cin>>name;
cout<<"Enter your Roll number:";
cin>>rollno;
cout<<"Enter Your Branch:";
cin>>branch;
cout<<"Enter Your Semester:";
cin>>sems;
}
void students::display()
{
cout<<"\nName:"<<name;
cout<<"\nRollno.:"<<rollno;
cout<<"\nBranch:"<<branch;
cout<<"\nSemester:"<<sems;
}
int main()
{
students s;
s.input();
s.display();
}

========================================================================

2. Implementation of member function using  inside class.

#include <iostream>
using namespace std;
class car
{
  private:
    int car_num;
    char car_model[10];
  public:
    void inputdata()
     {
       cout<<"Enter your car number: ";
       cin>>car_num;
       cout<<"\n Enter your car model: ";
       cin>>car_model;
     }
    void displaydata()
     {
       cout<<"Your Car number is "<<car_num;
       cout<<"\nYour Car model is "<<car_model;
     }
};
// main function starts
int main()
 {
    car c;
    c.inputdata();
    c.displaydata();
    return 0;
 }

========================================================================

3. Implementation of member function using  outside class.

#include <iostream>
using namespace std;
class car
{
  private:
    int car_num;
    char car_model[10];
  public:
    void inputdata(); //function declaration
    void displaydata();
};
// function definition
void car::inputdata()
 {
   cout<<"Enter your car number: ";
   cin>>car_num;
   cout<<"\n Enter your car model: ";
   cin>>car_model;
 }
void car::displaydata()
 {
   cout<<" Your Car number is "<<car_num;
   cout<<"\n Your Car model is "<<car_model;
 }
// main function starts
int main()
 {
    car c;
    c.inputdata();
    c.displaydata();
    return 0;
 }

========================================================================

4. WAP Array of  objects


#include<iostream>
using namespace std;

class Rectangle
{
public:
int length;
int breadth;
Rectangle( int l, int b )
{
length = l;
breadth = b;
}
int printArea()
{
return length * breadth;
}
};

int main()
{
Rectangle rt1( 4, 6 );
Rectangle rt2( 9, 2 );
cout << "Area of first rectangle " << rt1.printArea() << endl;
cout << "Area of second rectangle " << rt2.printArea() << endl;
return 0;
}

========================================================================

5. WAP for using copy constructor

#include<iostream>
using namespace std;
class Samplecopyconstructor
{
    private:
    int x, y;   //data members

    public:
    Samplecopyconstructor(int x1, int y1)
    {
        x = x1;
        y = y1;
    }

    /* Copy constructor */
    Samplecopyconstructor (const Samplecopyconstructor &sam)
    {
        x = sam.x;
        y = sam.y;
    }

    void display()
    {
        cout<<x<<" "<<y<<endl;
    }
};
/* main function */
int main()
{
    Samplecopyconstructor obj1(10, 15);     // Normal constructor
    Samplecopyconstructor obj2 = obj1;      // Copy constructor
    cout<<"Normal constructor : ";
    obj1.display();
    cout<<"Copy constructor : ";
    obj2.display();
    return 0;
}


========================================================================

6. WAP for using default constructor

// Cpp program to illustrate the
// concept of Constructors
#include <iostream>
using namespace std;

class construct {
public:
int a, b;

// Default Constructor
construct()
{
a = 10;
b = 20;
}
};

int main()
{
// Default constructor called automatically
// when the object is created
construct c;
cout << "a: " << c.a << endl
<< "b: " << c.b;
return 1;
}

========================================================================

7. WAP for using parameterized constructor

// CPP program to illustrate
// parameterized constructors
#include <iostream>
using namespace std;

class Point {
private:
int x, y;

public:
// Parameterized Constructor
Point(int x1, int y1)
{
x = x1;
y = y1;
}

int getX()
{
return x;
}
int getY()
{
return y;
}
};

int main()
{
// Constructor called
Point p1(10, 15);

// Access values assigned by constructor
cout << "p1.x = " << p1.getX() << ", p1.y = " << p1.getY();

return 0;
}

========================================================================

8. WAP for Destructors

#include <iostream>
using namespace std;
class HelloWorld{
public:
  //Constructor
  HelloWorld(){
    cout<<"Constructor is called"<<endl;
  }
  //Destructor
  ~HelloWorld(){
    cout<<"Destructor is called"<<endl;
   }
   //Member function
   void display(){
     cout<<"Hello World!"<<endl;
   }
};
int main(){
   //Object created
   HelloWorld obj;
   //Member function called
   obj.display();
   return 0;
}

========================================================================

10. WAP for implementation of Unary (--) operator overloading

#include <iostream>
using namespace std;

class Check
{
  private:
    int i;
  public:
    Check(): i(3) {  }
    Check operator -- ()
    {
        Check temp;
        temp.i = --i;
        return temp;
    }

    // Notice int inside barcket which indicates postfix decrement.
    Check operator -- (int)
    {
        Check temp;
        temp.i = i--;
        return temp;
    }

    void Display()
    { cout << "i = "<< i <<endl; }
};

int main()
{
    Check obj, obj1;
    obj.Display();
    obj1.Display();

    // Operator function is called, only then value of obj is assigned to obj1
    obj1 = --obj;
    obj.Display();
    obj1.Display();

    // Assigns value of obj to obj1, only then operator function is called.
    obj1 = obj--;
    obj.Display();
    obj1.Display();

    return 0;
}

========================================================================

11. WAP for implementation of Unary (++) operator 

overloading

11.1 Prefix Increment

#include <iostream>
using namespace std;

class Check
{
  private:
    int i;
  public:
    Check(): i(0) {  }

    // Return type is Check
    Check operator ++()
    {
       Check temp;
       ++i;
       temp.i = i;

       return temp;
    }

    void Display()
    { cout << "i = " << i << endl; }
};

int main()
{
    Check obj, obj1;
    obj.Display();
    obj1.Display();

    obj1 = ++obj;

    obj.Display();
    obj1.Display();

    return 0;
}




11.2 Prefix Increment

#include <iostream>
using namespace std;

class Check
{
  private:
    int i;
  public:
    Check(): i(0) {  }
    Check operator ++ ()
    {
        Check temp;
        temp.i = ++i;
        return temp;
    }

    // Notice int inside barcket which indicates postfix increment.
    Check operator ++ (int)
    {
        Check temp;
        temp.i = i++;
        return temp;
    }

    void Display()
    { cout << "i = "<< i <<endl; }
};

int main()
{
    Check obj, obj1;
    obj.Display();
    obj1.Display();

    // Operator function is called, only then value of obj is assigned to obj1
    obj1 = ++obj;
    obj.Display();
    obj1.Display();

    // Assigns value of obj to obj1, only then operator function is called.
    obj1 = obj++;
    obj.Display();
    obj1.Display();

    return 0;
}


========================================================================

12. WAP for implementation of Binary operator overloading

// C++ program to show binary operator overloading 
#include <iostream> 

using namespace std; 

class Distance { 
public: 
// Member Object 
int feet, inch; 
// No Parameter Constructor 
Distance() 
this->feet = 0; 
this->inch = 0; 

// Constructor to initialize the object's value 
// Parametrized Constructor 
Distance(int f, int i) 
this->feet = f; 
this->inch = i; 

// Overloading (+) operator to perform addition of 
// two distance object 
Distance operator+(Distance& d2) // Call by reference 
// Create an object to return 
Distance d3; 

// Perform addition of feet and inches 
d3.feet = this->feet + d2.feet; 
d3.inch = this->inch + d2.inch; 

// Return the resulting object 
return d3; 
}; 

// Driver Code 
int main() 
// Declaring and Initializing first object 
Distance d1(8, 9); 

// Declaring and Initializing second object 
Distance d2(10, 2); 

// Declaring third object 
Distance d3; 

// Use overloaded operator 
d3 = d1 + d2; 

// Display the result 
cout << "\nTotal Feet & Inches: " << d3.feet << "'" << d3.inch; 
return 0; 

========================================================================

13. Function Overloading

#include <iostream>
using namespace std;

void print(int i) {
cout << " Here is int " << i << endl;
}
void print(double f) {
cout << " Here is float " << f << endl;
}
void print(char const *c) {
cout << " Here is char* " << c << endl;
}

int main() {
print(10);
print(10.10);
print("ten");
return 0;
}

=============================================

14.Multilevel Inheritance

#include <iostream>
using namespace std;
class A {
public:
  A(){
     cout<<"Constructor of A class"<<endl;
  }
};
class B: public A {
public:
  B(){
     cout<<"Constructor of B class"<<endl;
  }
};
class C: public B {
public:
  C(){
     cout<<"Constructor of C class"<<endl;
  }
};
int main() {
  //Creating object of class C
  C obj;
  return 0;
}

=============================================

15. Hierarchical Inheritance

#include <iostream>
using namespace std;
class A {
public:
  A(){
     cout<<"Constructor of A class"<<endl;
  }
};
class B: public A {
public:
  B(){ 
     cout<<"Constructor of B class"<<endl;
  }
};
class C: public A{
public:
  C(){
     cout<<"Constructor of C class"<<endl;
  }
};
int main() {
   //Creating object of class C
   C obj;
   return 0;
}

=============================================

16.Multiple Iheritance

#include <iostream>
using namespace std;
class A {
public:
  A(){
     cout<<"Constructor of A class"<<endl;
  }
};
class B {
public:
  B(){
     cout<<"Constructor of B class"<<endl;
  }
};
class C: public A, public B {
public:
  C(){
     cout<<"Constructor of C class"<<endl;
  }
};
int main() {
   //Creating object of class C
   C obj;
   return 0;
}

=============================================

17.Single Inheritance

#include <iostream>
using namespace std;
class A {
public:
  A(){
     cout<<"Constructor of A class"<<endl;
  }
};
class B: public A {
public:
  B(){
     cout<<"Constructor of B class";
  }
};
int main() {
   //Creating object of class B
   B obj;
   return 0;
}

=============================================


18. Hybrid Inheritance

#include <iostream>
using namespace std;

class A
{
  public:
  int x;
};
class B : public A
{
  public:
  B()      //constructor to initialize x in base class A
  {
     x = 10;
  }
};
class C
 {
  public:
  int y;
  C()   //constructor to initialize y
  {
      y = 4;
        }
};
class D : public B, public C   //D is derived from class B and class C
{
  public:
  void sum()
  {
      cout << "Sum= " << x + y;
  }
};

int main()
{
         D obj1;          //object of derived class D
  obj1.sum();
  return 0;
}

=============================================

19.Virtual Funcctions

#include<iostream> 
using namespace std; 

class base 
public: 
virtual void print () 
{ cout<< "print base class" <<endl; } 

void show () 
{ cout<< "show base class" <<endl; } 
}; 

class derived:public base 
public: 
void print () 
{ cout<< "print derived class" <<endl; } 

void show () 
{ cout<< "show derived class" <<endl; } 
}; 

int main() 
base *bptr; 
derived d; 
bptr = &d; 
//virtual function, binded at runtime 
bptr->print(); 
// Non-virtual function, binded at compile time 
bptr->show(); 

=============================================

20. Ambiguity Problem


#include <iostream>
class base1

{

public:

base1(int data=0):m_data(data){}

int GetData()const{return(m_data);}

private:

int m_data;

};

class base2

{

public:

base2(int data=0):m_data(data){}

int GetData()const{return(m_data);}

private:

int m_data;

};

class derived : public base1, public base2

{

public:

derived(int data=0):base1(data),base2(data){}

};

int main()

{

derived d(5);

int x = d.GetData(); // Ambiguous!

return( 0 );

}

=============================================





Post a Comment

0 Comments