0% found this document useful (0 votes)
89 views

C++ Program

The document discusses several C++ programs: 1. A program to create a FLOAT class that overloads arithmetic operators to operate on FLOAT objects. 2. A program to create a STRING class with functions to initialize, get, display strings and overload operators like + and ==. 3. A program to create a SHAPE class with virtual functions to calculate area and perimeter of derived classes like SQUARE, RECTANGLE, TRIANGLE. 4. A program using function overloading to read matrices of different data types and find their sum. 5. A program to implement a stack data structure with functions to initialize, push, pop and check for overflow/underflow conditions.

Uploaded by

sridharan
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
89 views

C++ Program

The document discusses several C++ programs: 1. A program to create a FLOAT class that overloads arithmetic operators to operate on FLOAT objects. 2. A program to create a STRING class with functions to initialize, get, display strings and overload operators like + and ==. 3. A program to create a SHAPE class with virtual functions to calculate area and perimeter of derived classes like SQUARE, RECTANGLE, TRIANGLE. 4. A program using function overloading to read matrices of different data types and find their sum. 5. A program to implement a stack data structure with functions to initialize, push, pop and check for overflow/underflow conditions.

Uploaded by

sridharan
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 10

Write a C++ program to create a class FLOAT that contains one float data member.

Overload
all the four Arithmetic operators so that they operate on the object FLOAT
#include<iostream.h>

#include<conio.h>

class FLOAT

float n;

public:

FLOAT(){}

void getdata()

cout<<"\n ENTER AN FLOATING NUMBER :";

cin>>n;

void putdata()

cout<<"\n"<<n;

FLOAT operator+(FLOAT);

FLOAT operator*(FLOAT);

FLOAT operator-(FLOAT);

FLOAT operator/(FLOAT);

};

FLOAT FLOAT::operator+(FLOAT a)

FLOAT t;

t.n=n+a.n;

return t;
}

FLOAT FLOAT::operator*(FLOAT b)

FLOAT t;

t.n=n*b.n;

return t;

FLOAT FLOAT::operator-(FLOAT b)

FLOAT t;

t.n=n-b.n;

return t;

FLOAT FLOAT::operator/(FLOAT b)

FLOAT t;

t.n=n/b.n;

return t;

main()

clrscr();

FLOAT a,b,c;

a.getdata();

b.getdata();

c=a+b;

cout<<"\n ADDITION OF TWO OBJECTS";


c.putdata();

cout<<"\n MULTIPLICATION OF TWO OBJECTS";

c=a*b;

c.putdata();

cout<<"\n SUBSTRACTION OF TWO OBJECTS";

c=a-b;

c.putdata();

cout<<"\n DIVISION OF TWO OBJECTS";

c=a/b;

c.putdata();

getch();

Write a C++ program to create a class STRING. Write a Member Function to initialize,
get and display stings. Overload the operators ++ and == to concatenate two Strings and
to compare two strings respectively.
#include<iostream>
#include<string.h>

class String
{
        public:
                char str[20];
        public:
                void get()
                {
                        cout<<"\n Enter String              :   ";
                        cin>>str;
                }
                void display()
                {
                        cout<<str;
                }
            
   String operator+(String x)  //Concatenating String
                {
                        String s;
                        strcat(str,x.str);
                        strcpy(s.str,str);
                        return s;
                }
};
int main()
{
        String s1, s2, s3;
        s1.get();
        s2.get();
        cout<<"\n ----------------------------------------------";
        cout<<"\n\n First String is           :  ";
        s1.display ();   //Displaying First String
        cout<<"\n\n Second String is          :  ";
        s2.display ();  //Displaying Second String
        cout<<"\n ----------------------------------------------";
        s3=s1+s2;         //String is concatenated. Overloaded '+' operator
        cout<<"\n\n Concatenated String is    :  ";
        s3.display_string();
        return 0;
}

Write a C++ program to create a class SHAPE which consists of two VIRTUAL
FUNCTIONS Calculate_Area() and Calculate_Perimeter() to calculate area and
perimeter of various figures. Derive three classes SQUARE, RECTANGLE, TRIANGE
from class Shape and Calculate Area and Perimeter of each class separately and display
the result.

#include <iostream>

class Shape

protected:

double x, y;

public:
void set_dim(double i, double j=0)

x = i;

y = j;

virtual void show_area(void)

cout << "No area computation defined ";

cout << "for this class.\n";

};

class triangle : public Shape

public:

void show_area(void)

cout << "Triangle with height "<< x << " and base " << y<< " has an area of "<< x *
0.5 * y << ".\n”;

};

class square : public Shape

public:

void show_area(void)

cout << "Square with dimensions "<< x << "x" << y<< " has an area of "<< x * y
<< ".\n";

}
};

class circle : public Shape

public:

void show_area(void)

cout << "Circle with radius "<<x<<”an area of”<<3.14*x*x;

};

main(void)

Shape *p;

triangle t;

square s;

circle c;

p = &t;

p->set_dim(10.0, 5.0);

p->show_area();

p = &s;

p->set_dim(10.0, 5.0);

p->show_area();

p = &c;

p->set_dim(9.0);

p->show_area();

return 0;

}
Write a C++ program using Function Overloading to read two Matrices of different
Data Types such as integers and floating point numbers. Find out the sum of the above
two matrices separately and display the sum of these arrays individually

#include<iostream>

class matrix

int m,n;

public:

void get()

Cout<<”Enter Number of Rows \n”;

cin>>m;

cout<<”Enter Number of Columns \n”;

cin>>n;

void sum()

float a[10][10], b[10][10], c[10][10];

cout << "Enter the elements of  first 1st matrix: ";

for (int i = 0;i<m;i++ ) {

for (int j = 0;j < n;j++ ) {

cin>>a[i][j];

cout << "Enter the elements of  first 1st matrix: ";

for (int i = 0;i<m;i++ ) {

for (int j = 0;j<n;j++ ) {


cin>>b[i][j]; }

}}

void sum(void)

for (int i = 0;i<m;i++ ) {

for (int j = 0;j<n;j++ )

c[i][j]=a[i][j]+b[i][j];

cout<<c[i][j]<<" ";

};

int main()

matrix cp;

matrix cp1;

cout<<”Enter Integer Matrix \n”;

cp.get();

cp.sum();

cout<<”Display Integer Matrix \n”;

cp.sum(void);

cout<<”Enter float Matrix \n”;

cp1.sum();

cout<<”Display Addition of Float Matrix \n”;

cp1.sum(void);

return 0;

}
Write a C++ program to create a class to implement the data structure STACK. Write a
constructor to initialize the TOP of the STACK. Write a member function PUSH() to
insert an element and member function POP() to delete an element. Check for overflow
and underflow conditions

# include<iostream>
class Stack
{
int top;
public:
int a[10]; //Maximum size of Stack
Stack()
{
top = -1;
}
void push(int x);
int pop();
void isEmpty();
};
void Stack::push(int x)
{
if(top >= 10)
{
cout << "Stack Overflow \n";
}
else
{
a[++top] = x;
cout << "Element Inserted \n";
}
}
int Stack::pop()
{
if(top < 0)
{
cout << "Stack Underflow \n";
return 0;
}
else
{
int d = a[top--];
return d;
}
}
void Stack::isEmpty()
{
if(top < 0)
{
cout << "Stack is empty \n";
}
else
{
cout << "Stack is not empty \n";
}
}
int main() {

Stack s1;
s1.push(10);
s1.push(100);
}

You might also like