JNTUK R20 2-1 Object-Oriented Programming in C++ Lab

 JNTUK R20 2-1 Object-Oriented Programming in C++ Lab


1 a)   

Cpp program to create objects of distance class and add them



In this article we will learn to implement a Cpp program to create objects of distance class and add them. A C++ program is provided below that adds objects of distance class.

 

We create a distance class with the following:

  • feet and inches as data members
  • member function to input distance
  • member function to output distance
  • member function to add two distance objects of distance class

We create two objects of distance class and add them. Program is as follows:

#include <iostream>
using namespace std;

class Distance
{
private:
int feet;
int inches;
public:
void set_distance()
{
cout<<"Enter feet: ";
cin>>feet;
cout<<"Enter inches: ";
cin>>inches;
}

void get_distance()

{
cout<<"Distance is feet= "<<feet<<", inches= "<<inches<<endl;
}
void add(Distance d1, Distance d2)
{
feet = d1.feet + d2.feet;
inches = d1.inches + d2.inches;
feet = feet + (inches / 12);
inches = inches % 12;
}
};
int main()
{
Distance d1, d2, d3;
d1.set_distance();
d2.set_distance();
d3.add(d1, d2);
d3.get_distance();
return 0;
}



1 b)      

Cpp program to illustrate the use of constructor and destructor


In this article we will learn to implement a Cpp program to illustrate the use of constructor and destructor. A C++ program is provided below to demonstrate the use of constructor and destructors. Program is as follows:

#include <iostream>
using namespace std;
class Distance
{
private:
int feet;
int inches;
public:
Distance() {}
Distance(int f, int i)
{
feet = f;
inches = i;
}
void get_distance()
{
cout<<"Distance is feet= "<<feet<<", inches= "<<inches<<endl;
}
void add(Distance &d1, Distance &d2)
{
feet = d1.feet + d2.feet;
inches = d1.inches + d2.inches;
feet = feet + (inches / 12);
inches = inches % 12;
}
~Distance()
{
cout<<"Distance object destroyed"<<endl;
}
};
int main()
{
int f1, in1, f2, in2;
cout<<"Enter feet: ";
cin>>f1;
cout<<"Enter inches: ";
cin>>in1;
cout<<"Enter feet: ";
cin>>f2;
cout<<"Enter inches: ";
cin>>in2;
Distance d1(f1, in1);
Distance d2(f2, in2);
Distance d3;
d3.add(d1, d2);
d3.get_distance();
return 0;
}


Input and output for the above program are as follows:



1 c)

CPP program for illustrating function overloading in adding the distance between objects


In this article we will learn to implement a CPP program for illustrating function overloading in adding the distance between objects. A C++ program is provided below for adding the distance between objects using function overloading.

 

We create a distance class with the following:

  • feet and inches as data members
  • member function to input distance
  • member function to output distance
  • member function to add two distance objects

 

Now we overload the add function for adding two distance objects. Program is as follows:


#include <iostream>

using namespace std;

class Distance

{

private:

int feet;

int inches;

public:

void set_distance()

{

cout<<"Enter feet: ";

cin>>feet;

cout<<"Enter inches: ";

cin>>inches;

}

void get_distance()

{

cout<<"Distance is feet= "<<feet<<", inches= "<<inches<<endl;

}

void add(Distance d1, Distance d2)

{

feet = d1.feet + d2.feet;

inches = d1.inches + d2.inches;

feet = feet + (inches / 12);

inches = inches % 12;

}

void add(Distance *d1, Distance *d2)

{

feet = d1->feet + d2->feet;

inches = d1->inches + d2->inches;

feet = feet + (inches / 12);

inches = inches % 12;

}

};

int main()

{

Distance d1, d2, d3;

d1.set_distance();

d2.set_distance();

d3.add(d1, d2);

d3.get_distance();

d3.add(&d1, &d2);

d3.get_distance();

return 0;

}


Input and output for the above program are a follows:





2 a)  CPP program using friend function

#include <iostream>
using namespace std;
class A
{
private:
int x;
public:
A(int p)
{
x = p;
}
friend void display(A &);
};
void display(A &obj)
{
cout<<"x = "<<obj.x;
}
int main()
{
A obj(10);
display(obj);
return 0;
}


Output for the above program is as follows:

x = 10



2. b) CPP program to illustrate this pointer

In this article we will learn to implement a CPP program to illustrate this pointer. A C++ program is provided below to demonstrate this pointer.


Program is as follows:


#include <iostream>
using namespace std;
class A
{
private:
int x;
int y;
public:
A(int x, int y)
{
this->x = x;
this->y = y;
}
void display()
{
cout<<"x = "<<x<<endl;
cout<<"y = "<<y<<endl;
}
A& clone()
{
return *this;
}
};
int main()
{
A obj1(10, 20);
obj1.display();
A obj2 = obj1.clone();
obj2.display();
return 0;
}

Output for the above program is as follows:



2.c) CPP program to illustrate pointer to a class


In this article we will learn to implement a CPP program to illustrate pointer to a class. A C++ program is given below to illustrate pointer to a class.


Program is as follows:


#include <iostream>
using namespace std;
class A
{
private:
int x;
int y;
public:
A(int x, int y)
{
this->x = x;
this->y = y;
}
void display()
{
cout<<"x = "<<x<<endl;
cout<<"y = "<<y<<endl;
}
};
int main()
{
A *ptr = new A(10, 30); //Here ptr is pointer to class A
ptr->display();
return 0;
}

Output for the above program is as follows:




CPP program to overload unary operator using member function

In this article we will learn to implement a CPP program to overload unary operator using member function. A C++ program is overloaded to overload unary operator using member function.

Program is as follows:

#include <iostream>
using namespace std;
class Number
{
private:
int x;
public:
Number(int p)
{
x = p;
}
void operator -()
{
x = -x;
}
void display()
{
cout<<"x = "<<x;
}
};
int main()
{
Number n(10);
-n;
n.display();
return 0;
}

Output for the above program is as follows:

x = -10

CPP program to overload binary operator using non member function


In this article we will learn to implement a CPP program to overload binary operator using non member function. A C++ program is provided below to overload binary operator.


#include <iostream>
using namespace std;
class Complex
{
private:
float real;
float imag;
public:
Complex(){}
Complex(float r, float i)
{
real = r;
imag = i;
}
void display()
{
cout<<real<<"+i"<<imag;
}
friend Complex operator +(Complex &, Complex &);
};
Complex operator +(Complex &c1, Complex &c2)
{
Complex temp;
temp.real = c1.real + c2.real;
temp.imag = c1.imag + c2.imag;
return temp;
}
int main()
{
Complex c1(3, 4);
Complex c2(4, 6);
Complex c3 = c1+c2;
c3.display();
return 0;
}

Output of the program:

7+i10


CPP program to overload assignment operator


#include <iostream>
using namespace std;
class Number
{
private:
int x;
public:
Number(int p)
{
x = p;
}
Number operator =(Number &n)
{
return Number(n.x);
}
void display()
{
cout<<"x = "<<x;
}
};
int main()
{
Number n1(10);
Number n2 = n1;
n2.display();
return 0;
}


Output for the above program is as follows:

x = 10


No comments

JavaFX Scene Builder

  This is an article about the JavaFX Scene Builder. You will get a short introduction about the installation and usage of the software. The...