If, you are interested in C Language. Click here  

C++  OOP

Program No. 1

1)           Write a  Program that declares a class with one integer data member and two members in() and out() input and output data members.

Coding

#include<iostream>
using namespace std;
class test
{
private:
    int n;
public:
void in()
    {
    cout<<"Enter a number:";
    cin>>n;
    }
    void out()
    {
    cout<<"The value of n:"<<n;
    }

};
int main()
{
test obj;
obj.in();
obj.out();
return 0;
}


___________________________________________________________________________________
                                  __________________________________________________

Program No. 2

2)             Write a Program that creates a class with marks with three data members to store three marks. Write three members function in() to input marks and sum to calculate the marks and average calculate to average marks.

coding


#include<iostream>
using namespace std;
class test
{
private:
int a,b,c;
public:
void in()
{
cout<<"Enter a 1st number:";
cin>>a;
cout<<"Enter a 2nd number:";
cin>>b;
cout<<"Enter a 3rd number:";
cin>>c;
}
int sum()
{
return a+b+c;
}
float avg()
{
return (a+b+c)/3;
}
};
int main()
{
test marks;
int s;
float a;
marks.in();
s=marks.sum();
a=marks.avg();
cout<<"Sum="<<s<<endl;
cout<<"Average="<<a;
return 0;
}


___________________________________________________________________________________
                                  __________________________________________________

Program No. 3



3) -          Write a program that creates a class with three data members; book id, pages and price. It also contains the following function get( ) is used to the input value, the show( ) is used to display value. The set( ) is used to set the values of data members using parameters and get price( ) is used to return the value of the Price. The program should 2 objects of the class cans input value of these objects.

coding


#include<iostream>
using namespace std;
class book
{
private:
int id,pages;
float price;
public:
void get()
{
cout<<"Enter book id:";
cin>>id;
cout<<"Enter book pages:";
cin>>pages;
cout<<"Enter book price:";
cin>>price;
}
void show()
{
cout<<"book id="<<id<<endl;
cout<<"book pages="<<pages<<endl;
cout<<"book price="<<price<<endl;
}
void set(int bookid, int pg, int pr)
{
id=bookid;
pages=pg;
price=pr;
}
int getprice()
{
return price;
}
};
int main()
{
book b1,b2;
b1.get();
b2.set(10,50,105.50);
cout<<endl<<"The detail of the most costly book is as follows="<<endl;
if(b1.getprice()>b2.getprice())
b1.show();
else
b2.show();
return 0;
}



___________________________________________________________________________________
                                  __________________________________________________

Program No. 3


4)               Find the maximum and minimum elements in an array in OOP in C++.

Coding

#include<iostream>
using namespace std;
class array
{
private:
int a[5];
public:
void fill();
void display();
int max();
int min();
};
void array::fill()
{
for(int i=0; i<5; i++)
{
cout<<"Enter a["<<i<<"]";
cin>>a[i];
}
}
void array::display()
{
for(int i=0; i<5; i++)
{
cout<<"a["<<i<<"]"<<a[i]<<endl;
}
}
int array::max()
{
int m=a[0];
for(int i=0; i<5; i++)
if(m<a[i])
m=a[i];
return m;
}
int array::min()
{
int m=a[0];
for(int i=0; i<5; i++)
if(m>a[i])
m=a[i];
return m;
}
int main()
{
array arr;
arr.fill();
cout<<"max = "<<arr.max()<<endl;
cout<<"min = "<<arr.min()<<endl;
return 0;
}


___________________________________________________________________________________
                                  __________________________________________________

Or Download the devC++.cpp file. 

______________________________________________________________________________________________________________________________________________________________________

Assignment  No 1

Thank You