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

Practical File Format - OOPC

This document contains details about the Object Oriented Programming with C++ course including the course code, semester, academic year, and a list of programming assignments. The programming assignments cover topics like using cout and endl manipulator, arrays, pointers, recursion, references, and dynamic memory allocation. For each assignment, the problem statement, sample code, output and conclusion are provided. The assignments help learn concepts like formatting output, arrays, recursion, references, and memory management in C++.

Uploaded by

Malav Patel
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
182 views

Practical File Format - OOPC

This document contains details about the Object Oriented Programming with C++ course including the course code, semester, academic year, and a list of programming assignments. The programming assignments cover topics like using cout and endl manipulator, arrays, pointers, recursion, references, and dynamic memory allocation. For each assignment, the problem statement, sample code, output and conclusion are provided. The assignments help learn concepts like formatting output, arrays, recursion, references, and memory management in C++.

Uploaded by

Malav Patel
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 25

Object Oriented Programming with C++ [CE144]

ID No.: 20DIT059

CHAROTAR UNIVERSITY OF SCIENCE & TECHNOLOGY

DEVANG PATEL INSTITUTE OF ADVANCE TECHNOLOGY & RESEARCH

Information Technology

Subject Name: Object Oriented Programming with C++


Semester: II
Subject Code: CE144
Academic year: 2020-21

Practical List
No. Aim of the Practical
1. Write a C++ program that will print output in the following form. Make sure your output looks
exactly as shown here (including spacing, line breaks, punctuation, and the title and author). Use
cout and cin objects and endl manipulator.
******************************
* Programmimg Assignment 1 *
* Computer Programmimg I *
* Author : ??? *
* Due Date: Thursday, Dec. 20 *
******************************

PROGRAM CODE :
#include <iostream>
using namespace std;
int main(){
    cout<<"******************************\n* Programmimg Assignment 1    *\n* C
omputer Programmimg I      *\n* Author : ???                *\n* Due Date: Thur
sday, Dec. 20 *\n******************************";
    cout<<"\n\n\n                                         ---- Name : Malav Pat
el\n                                              ID   : 20DIT059\n";

return 0;
}

1
Object Oriented Programming with C++ [CE144]
ID No.: 20DIT059

OUTPUT:

CONCLUSION: In This program I learned How to use cout , \n , And endl;

Que 2 : Write a program to create the following table.


Use endl and setw manipulator
PROGRAM CODE :
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
    int n;
    cout << "Enter the number: ";
    cin >> n;
    for(int i = 1; i<=n; i++)
    {

        for(int z = 1; z <= n; z++)
        {
            cout << "-----";
        }
        cout << "-";
        cout << endl;
        cout << "|";
        for(int j = 1; j<=n; j++)
        {
            cout << setw(3);
            cout << i*j <<" |" ;
        }
        cout << endl ;
    }
    for(int z = 1; z <= n; z++)
    {
        cout << "-----";
    }
    cout << "-";
    cout << "\n\n\n                                         ---- Name : Malav Pa
tel\n                                              ID   : 20DIT059\n";

2
Object Oriented Programming with C++ [CE144]
ID No.: 20DIT059

OUTPUT:

CONCLUSION:
In The program I learned setw .setw is used for set the width in output

Que 3 : Write a C++ program to add two floating numbers using pointer. The resultshould
contain only two digits after the decimal. Use fixed, scientific and setprecision () manipulators for
controlling the precision of floating point numbers.
PROGRAM CODE :
#include <iostream>
#include<iomanip>
using namespace std;

int main(){
    float a,b,*p,*q;
    cout<<"Enter Two Float Number \n1 : ";
    cin>>a;
    cout<<"2 : ";
    cin>>b;
    p = &a;
    q = &b;
    cout<<"Setprecision              Sum              Fixed             scientif
ic  "<<endl;
    cout<<"    1       "<<setw(15)<<setprecision(1)<<*p+*q<<"     "<<setw(15)<<f
ixed<<*p+*q<<"       "<<setw(15)<<scientific<<*p+*q<<endl;
    cout<<"    2       "<<setw(15)<<setprecision(2)<<*p+*q<<"     "<<setw(15)<<f
ixed<<*p+*q<<"       "<<setw(15)<<scientific<<*p+*q<<endl;
    cout<<"    3       "<<setw(15)<<setprecision(3)<<*p+*q<<"     "<<setw(15)<<f
ixed<<*p+*q<<"       "<<setw(15)<<scientific<<*p+*q<<endl;
    cout<<"    4       "<<setw(15)<<setprecision(4)<<*p+*q<<"     "<<setw(15)<<f
ixed<<*p+*q<<"       "<<setw(15)<<scientific<<*p+*q<<endl;
    cout<<"    5       "<<setw(15)<<setprecision(5)<<*p+*q<<"     "<<setw(15)<<f
ixed<<*p+*q<<"       "<<setw(15)<<scientific<<*p+*q<<endl;

3
Object Oriented Programming with C++ [CE144]
ID No.: 20DIT059

    // cout<<"\n\n\nName : Malav Patel\nID : 20DIT059\n";
    cout<<"\n\n\n                                                             -- 
Name : Malav Patel\n                                                             
   ID   : 20DIT059\n";

return 0;
}

OUTPUT:

CONCLUSION:
 The use of set precision method to format the output of the floating point
output using fixed and scientific notations for the significant figures after a
decimal point is also learned.

Que 4 : Write a C++ program to find out sum of array element using Recursion. Question:
Show stepwise solution of winding and unwinding phase of recursion

PROGRAM CODE :
#include <iostream>
using namespace std;
int num;
int sum(int arr[])
{
    if (num == 0)
    {
        return 0;
    }
    num--;
    return arr[num] + sum(arr);
}
int main()
{
    cout << "\n\nHow Many Number You Want To Enter : ";
    cin >> num;
    int arr[num];
    cout<<"\n";
    for (int i = 0; i < num; i++)
    {

4
Object Oriented Programming with C++ [CE144]
ID No.: 20DIT059

        cout <<"arr["<< i + 1 << "] : ";
        cin >> arr[i];
    }
    cout<<"\nSum Of Array  : "<<sum(arr);
    cout << "\n\n\n                                         ---- Name : Malav Pa
tel\n                                              ID   : 20DIT059\n\n\n";

    return 0;
}

OUTPUT:

CONCLUSION:
In this program I learned Recursion.

Que 5 : Find error in the following code and give reasons for each error :

PROGRAM CODE :
#include <iostream>
using namespace std;
int main()
{
    int no1 = 10, no2 = 12;
    int &x = no1;
     // Error :-
//int &r; // r is declared as reference but not initialized.
    // int &c = NULL; // we can not store NULL in &c.
    //int &d[2] = {no1 , no2};// we can not make reference array
    cout << "\nx    = " << x + 20;
    cout << "\nno1  = " << no1 + 10;
    return 0;
}

5
Object Oriented Programming with C++ [CE144]
ID No.: 20DIT059

CONCLUSION:
 Can we declare an array of references?
Ans : We cannot point reference variable to an array because array elements always
point to an index address.

 Can we assign NULL value to reference variable?


Ans : No , Because Reference variable store address and Null is a Not a valid address.

 Is Reference variable a pointer variable?


Ans : No, it can’t be pointer because reference variable can be referenced by pass by
value. References are used to refer an existing variable.

 Can we declare a reference variable without initializing it?


Ans : No , It’s Mandatory to be initialized a reference variable.

 Does Reference Variable change the original value of variable?


Ans : Yes, Reference Variable changes the original Value & used to modify original data.

Que 6 : Find output of the following code: Explain how scope Resolution operator is used to
access global version of a variable.
PROGRAM CODE :
#include <iostream>
#include <conio.h>
using namespace std;
int m = 30;
int main(){
    int m = 20;
    {
        int m = 10;
        cout <<"we are in inner block"<< endl;
        cout <<"value of m ="<< m <<"\n";
        cout <<"value of ::m ="<< ::m <<"\n";
    }
    cout <<"we are in outer block"<< endl;
    cout <<"value of m ="<< m <<"\n";
    cout <<"value of ::m ="<< ::m <<"\n";
    cout << "\n\n\n                                         ---- Name : Malav Pa
tel\n                                              ID   : 20DIT059\n\n\n";
return 0;
}
OUTPUT:

6
Object Oriented Programming with C++ [CE144]
ID No.: 20DIT059

CONCLUSION: we have study that if the global variable name is same as local variable
name, the scope resolution operator will be used to call the global variable. It is also used to
define a function outside the class and used to access the static variable of class.

Que 7: Write a program to enter a size of array. Create an array of size given by user using
“new” Dynamic memory management operator (free store operator). Enter the data to store in
array and display the data after adding 2 to each element in the array. Delete the array by using
“delete” memory management operator.
PROGRAM CODE :
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
    int n;
    cout<<"Enter Total Number : ";
    cin>>n;
    int *arr = new int[n];
    for (int i = 0; i < n; i++)
    {
        cout<<i+1<<" : ";
        cin>>arr[i];
    }
    cout<<"\nBefore Adding :: \n";
    for (int i = 0; i < n; i++)
    {
        cout<<arr[i]<<" ";
    }
    cout<<"\nAfter Adding :: \n";
    for (int i = 0; i < n; i++)
    {
        cout<<arr[i]+2<<" ";
    }
    delete[] arr;
    cout << "\n\n\n                                         ---- Name : Malav Pa
tel\n                                              ID   : 20DIT059\n\n\n";
    return 0;
}

7
Object Oriented Programming with C++ [CE144]
ID No.: 20DIT059

OUTPUT:

CONCLUSION: Memory that is dynamically allocated using the new operator can be freed
using the delete operator. The delete operator calls the operator delete function, which frees
memory back to the available pool.

Que 8: Find Error in the following code of a program and give explanation why these errors
exist.
8 a ::
#include <iostream>
using namespace std;
int main()
{
    int var1 = 35, var2 = 20;
    int *const ptr = &var1;
    // ptr = &var2; //The pointer cannot be assigned the constant value twice
    cout << "var1= " << *ptr;
    return 0;}
8 b ::
#include <iostream>
using namespace std;
int main()
{
    int var1 = 43;
    const int *ptr = &var1;
    // *ptr = 1;// the value of pointer can't be assign twice
    var1 = 34;
    cout << "var1 = " << *ptr;
    return 0;
}
//constant variable cannot be initializied by any constant

8 c ::
#include <iostream>
using namespace std;

8
Object Oriented Programming with C++ [CE144]
ID No.: 20DIT059

int main()
{
    int var1 = 0, var2 = 0;
    const int *const ptr = &var1;
    // *ptr = 1;  //The pointer cannot be assigned the constant value twice
    // ptr = &var2;
    cout << "Var1 = " << *ptr;
    return 0;
}

CONCLUSION: In this practical we studied that we cannot assign another variable address
to constant pointer variable, A const pointer always points to the same address, and this
address cannot be changed. A constant pointer to constant is a pointer that can neither change
the address its pointing to and nor it can change the value kept at that address

Que 9: Find the output of following program


PROGRAM CODE :
#include <iostream>
using namespace std;
int main()
{
    bool a = 321, b;
    cout << "Bool a Contains : " << a << endl;
    int c = true;
    int d = false;
    cout << "c = " << c << endl << "d = " << d;
    c = a + a;
    cout << "\nInteger c contain : " << c;
    b = c + a;
    cout << "\nBool b contain : " << b;
    cout << "\n\n\n                                         ---- Name : Malav Pa
tel\n                                              ID   : 20DIT059\n\n\n";
    return 0;
}

OUTPUT:

9
Object Oriented Programming with C++ [CE144]
ID No.: 20DIT059

CONCLUSION: A Boolean data type is defined using bool. Usually , 1(true) and 2(false)
are assigned to Boolean variable as their default numerical values.

Que 10: Define three functions named divide (). First function takes numerator and
denominator as an input argument and checks it’s divisible or not, second function takes one int
number as input argument and checks whether the number is prime or not and Third function
takes 3 float number as argument and finds out average of the numbers. Use concept of
Function Overloading / static binding.
PROGRAM CODE:
#include <iostream>
using namespace std;
void divide(int n, int d)
{
    if (n % d == 0)
    {
        cout << "It is divisible\n"
             << n << " / " << d << " = " << n / d;
    }
    else
    {
        cout << "\nIt is Not Divisible\n";
    }
}
void divide(int num)
{
    int i, m = 0, flag = 0;
    m = num / 2;
    for (i = 2; i <= m; i++)
    {
        if (num % i == 0)
        {
            cout << "\nNumber is not Prime." << endl;
            flag = 1;
            break;
        }
    }
    if (flag == 0)
        cout <<"\nNumber is Prime." << endl;
}
void divide(float a, float b , float c)
{

10
Object Oriented Programming with C++ [CE144]
ID No.: 20DIT059

    cout<<"\nAverage Sum : "<<(a+b+c)/3<<endl;
}
int main()
{
    int casenum;
    cout<<"\n1. Check Prime Number\n2. Check Two Number Divisble Or Not\n3. Aver
age of 3 Number\nEnter Your choice : ";
    cin>>casenum;
    switch (casenum)
    {
    case 1:
        int num;
        cout<<"\nEnter A Integer Number : ";
        cin>>num;
        divide(num);
        break;
    case 2:
        int num1 , num2;
        cout<<"\nEnter a Numerator Number   : ";
        cin>>num1;
        cout<<"Enter a Denominator Number : ";
        cin>>num2;
        divide(num1,num2);
        break;
    case 3:
        float num3,num4,num5;
        cout<<"\nEnter Three Number :: \n";
        cout<<"1 : ";
        cin>>num3;
        cout<<"2 : ";
        cin>>num4;
        cout<<"3 : ";
        cin>>num5;
        divide(num3,num4,num5);
        break;
    }
    cout << "\n\n\n                                         ---- Name : Malav Pa
tel\n                                              ID   : 20DIT059\n\n\n";
    return 0;
}

OUTPUT 1:

11
Object Oriented Programming with C++ [CE144]
ID No.: 20DIT059

OUTPUT 2:

OUTPUT 3:

12
Object Oriented Programming with C++ [CE144]
ID No.: 20DIT059

CONCLUSION: In this practical we studied that Function overloading is a programming


concept that allows programmers to define two or more functions with the same name and in
the same scope.

Que 11: Write a function called tonLarge() that takes two integer arguments call by reference
and then sets the larger of the two numbers to 100 using Return by reference. Write a main()
program to exercise this function.
PROGRAM CODE:
#include <iostream>
using namespace std;
void tonLarge(int *a , int* b){
    if (*a > *b)
    {
        *a = 100;
    }
    else
    {
        *b = 100;
    }
    
}
int main(){
    int num1,num2;
    cout<<"Enter Frist Number  : ";
    cin>>num1;
    cout<<"Enter Second Number : ";
    cin>>num2;
    tonLarge(&num1,&num2);
    cout<<"Number 1 : "<<num1<<endl;
    cout<<"Number 2 : "<<num2<<endl;
    cout << "\n\n\n                                         ---- Name : Malav Pa
tel\n                                              ID   : 20DIT059\n\n\n";
return 0;
}
OUTPUT :

13
Object Oriented Programming with C++ [CE144]
ID No.: 20DIT059

CONCLUSION: In this practical we studies that the call by reference method of passing
arguments to a function copies the reference of an argument into the formal parameter.

Que 12: Write a function called power () that takes two arguments: a double value for n and
an int for p, and returns the result as double value. Use default argument of 2 for p, so that if this
argument is omitted, the number will be squared. Write a main () function that gets values from
the user to test this function.
PROGRAM CODE:
#include <iostream>
using namespace std;
int power(double n, int p)
{
    double num = n;
    for (int i = 1; i < p; i++)
    {
        n = n * num;
    }
    return n;
}
int power(double n)
{
    int p = 2;
    double num = n;
    for (int i = 1; i < p; i++)
    {
        n = n * num;
    }
    return n;
}
int main()
{
    int p;
    double n, num;
    cout << "Enter Base Number : ";
    cin >> n;
    cout << "Enter The Power   : ";

14
Object Oriented Programming with C++ [CE144]
ID No.: 20DIT059

    cin >> p;
    if (p == 0)
    {
        cout <<"Ans : "<< power(n);
    }
    else
    {
        cout <<"Ans : "<< power(n, p);
    }
    cout << "\n                                         ---- Name : Malav Patel\n                                              ID   : 2
0DIT059\n\n\n";
    return 0;
}

OUTPUT :

CONCLUSION: In this practical we learned function is call from it’s parameter .

Que 13: Define a C++ Structure Rectangle with data member’s width and height. It has
get_values() member functions to get the data from user and area() member functions to print
the area of rectangle. Also create a C++ Class for the above program. Define both functions
inside the class. Member function defined inside the class behaves like an inline function and
illustrate the difference between C++ Structure and C++ Class.

PROGRAM CODE 1:
#include<iostream>
using namespace std;
struct Rectangle
{
 float width,height,area;
void get_value()
 {
  cout<<"Enter the width :";
  cin>>width;
  cout<<"Enter the height :";
  cin>>height;
 }
 void area1()
 {
  area = width*height;
  cout<<"Area of Rectangle is :"<<area;
 }
}t;
int main()
{

15
Object Oriented Programming with C++ [CE144]
ID No.: 20DIT059

 Rectangle t;

 t.get_value();
 t.area1();
 
    cout << "\n\n                                                             -- Name : Malav Patel\n                                     
                           ID   : 20DIT059\n\n";
 return 0;
}
OUTPUT :

PROGRAM CODE 2:
#include <iostream>
using namespace std;
class rectangle{
    int height,width;
    public:
    void get_valeus(int a, int b){
        height = a;
        width = b;
    }
    void area(){
        cout<<"\n\nArea Of Rectancle : "<<height*width<<endl;
    }
};
int main(){
    class rectangle r1;
    int a,b;
    cout<<"\n\nEnter Height of rectangle : ";
    cin>>a;
    cout<<"\nEnter Width  of rectangle : ";
    cin>>b;
    r1.get_valeus(a,b);
    r1.area();
    cout<<"\n\n                                                             -- Name : Malav Patel\n                                       
                         ID   : 20DIT059\n\n";
return 0;
}

OUTPUT :

16
Object Oriented Programming with C++ [CE144]
ID No.: 20DIT059

CONCLUSION: n this practical we learned what is the difference between structure and
class.

Que 14: Write a C++ program having class Batsman. It has private data members: batsman
name, bcode (4 Digit Code Number), innings, not out, runs, batting average. Innings, not out
and runs are in integer and batting average is in float.Define following function outside the class
using scope resolution operator. 1) Public member function getdata()to read values of data
members. 2) Public member function putdata()to display values of data members. 3) Private
member function calcavg() which calculates the batting average of a batsman. Also make this
outside function inline. Hint : batting average = runs/(innings - notout)
PROGRAM CODE:
#include <iostream>
#include <string>
using namespace std;
class batsman
{
    int Innings, notout, runs, bcode;
    float battingaverage;
    string name;
    void calcavg()
    {
        battingaverage = (float)runs / (Innings - notout);
    }

public:
    void getdata(int a, int b, int c, int d, string name1);
    void putdata();
};
void batsman ::getdata(int a, int b, int c, int d, string name1)
{
    Innings = a;
    notout = b;
    runs = c;
    bcode = d;
    name = name1;
    calcavg();
}
void batsman ::putdata()
{
    cout << "\n-----------------------------------------------------------------------------------------------
-------------------------";
    cout << "\n\n                           :: Player detail :: \n\n";
    cout << "                        Name            :  " << name << endl;
    cout << "                        B Code          :  " << bcode << endl;

17
Object Oriented Programming with C++ [CE144]
ID No.: 20DIT059

    cout << "                        Innings         :  " << Innings << endl;
    cout << "                        Not Out         :  " << notout << endl;
    cout << "                        Runs            :  " << runs << endl;
    cout << "                        Batting Average :  " << battingaverage << endl;
}
int main()
{
    class batsman player;
    string name;
    int a, b, c, d;
    cout << "\n\nName           :  ";
    cin >> name;
    cout << "B Code         :  ";
    cin >> d;
    cout << "Innings        :  ";
    cin >> a;
    cout << "Not Out        :  ";
    cin >> b;
    cout << "Runs           :  ";
    cin >> c;
    player.getdata(a, b, c, d, name);
    player.putdata();
    cout << "\n\n                                                             -- Name : Malav Patel\n                                     
                           ID   : 20DIT059\n\n";
    return 0;
}
OUTPUT :

CONCLUSION: In this practical we learned how to make private member function and we
have to call it from another member function which is publicly defined.

Que 15: Define class Currency having two integer data members rupee and paisa. A class
has member functions enter() to get the data and show() to print the amount in 22.50 format.
Define one member function sum() that adds two objects of the class and stores answer in the
third object i.e. c3=c1.sum (c2). The second member function should add two objects of type
currency passed as arguments such that it supports c3.add(c1,c2); where c1, c2 and c3 are
objects of class Currency. Also Validate your answer if paisa >100. Write a main( )program to
test all the functions. Use concepts of Object as Function Arguments, function returning object
and function overloading.

PROGRAM CODE:
#include<iostream>

18
Object Oriented Programming with C++ [CE144]
ID No.: 20DIT059

#include<iomanip>
using namespace std;
class Currency
{
public:
    int rupee;
    int paisa;
    float amt;
    void enter()
     {
     cout<<"Enter currency in Rupee: ";
     cin>>rupee;
     cout<<"Enter currency in Paise: ";
     cin>>paisa;
     }
     void show()
     {
         if(paisa>=100)
         {
             rupee = rupee + paisa/100;
             paisa = paisa%100;
         }
         amt = rupee + (float)paisa/100;
         cout<<"Amount : "<<setprecision(2)<<fixed<<amt<<endl;
     }
     Currency sum(Currency ob)
     {
         Currency t;
         t.rupee = rupee + ob.rupee;
         t.paisa = paisa + ob.paisa;
         return t;
     }
     void add(Currency ob1, Currency ob2)
     {
         Currency t;
         t.rupee = ob1.rupee + ob2.rupee;
         t.paisa = ob1.rupee + ob2.paisa;
     }
};

int main()
{
    Currency c1;
    Currency c2;
    Currency c3;
    c1.enter();
    c2.enter();
    c3=c1.sum(c2);
    c3.add(c1,c2);
    c3.show();
    cout << "\n\n                                                             -- Name : Malav Patel\n                                     
                           ID   : 20DIT059\n\n";
    return 0;
}

OUTPUT :

19
Object Oriented Programming with C++ [CE144]
ID No.: 20DIT059

CONCLUSION: In this practical we learned how to get the sum of two different objects
which are entered by the user.

Que 16: Define a class Dist with int feet and float inches. Define member function that
displays distance in 1’-2.5” format. Also define member function scale ( ) function that takes
object by reference and scale factor in float as an input argument. The function will scale the
distance accordingly. For example, 20’-5.5” and Scale Factor is 0.5 then answer is 10’-2.75”

PROGRAM CODE:
#include<iostream>
#include<stdio.h>
using namespace std;
class dist
{
    float inches;
    float feet;
    public:
    void getdata()
    {
        cout<<"ENTER THE FEET : ";
        cin>>feet;
        cout<<"ENTER THE INCHES : ";
        cin>>inches;
    }
    void putdata()
    {
        cout<<feet<<"'"<<inches<<"''";
    }
    void scale(dist &a,float b)
    {
        a.feet*=b;
        a.inches*=b;
    }
};
int main()
{
    dist d1;
    float f;
    d1.getdata();
    cout<<"ENTER SCALE VALUE : ";
    cin>>f;

20
Object Oriented Programming with C++ [CE144]
ID No.: 20DIT059

    d1.scale(d1,f);
    d1.putdata();
    cout << "\n\n                                                             -- Name : Malav Patel\n                                     
                           ID   : 20DIT059\n\n";
}
OUTPUT :

CONCLUSION: In this practical we learned how to call parameterised function for objects

Que 17: Create a Class Gate for students appearing in Gate (Graduate Aptitude test for
Engineering) exam. There are three examination center Vadodara, Surat, and Ahmedabad
where Gate exams are conducted. A class has data members: Registration number, Name of
student, Examination center. Class also Contains static data member ECV_Cnt, ECS_Cnt and
ECA_Cnt which counts the number of students in Vadodara, Surat and Ahmedabad exam
center respectively. Class Contains two Member function getdata () which gets all information of
students and counts total students in each exam center and pudata () which prints all
information about the students. Class also contains one static member function getcount ()
which displays the total number of students in each examination center. Write a program for 5
students and display the total number of students in each examination center. Use static data
member, static member function and Array of Objects.
PROGRAM CODE:
#include<iostream>
#include<string>
using namespace std;
class Gate
{
public:
    int regn;
    string name;
    string examcenter;
    static int ECV_Cnt;
    static int ECS_Cnt;
    static int ECA_Cnt;
    void getdata()
    {
        int c;
        cout<<"Enter the name of student : ";
        cin>>name;
        cout<<"Enter registration number : ";
        cin>>regn;
        cout<<"Examination center : \n1.Vadodara\n2.Surat\n3.Ahmedabad\nEnter you choice : ";
        cin>>c;
        switch(c)
        {
        case 1:
            examcenter = "Vadodara";
            ECV_Cnt++;
            break;
        case 2:
            examcenter = "Surat";

21
Object Oriented Programming with C++ [CE144]
ID No.: 20DIT059

            ECS_Cnt++;
            break;
        case 3:
            examcenter = "Ahmedabad";
            ECA_Cnt++;
            break;
        default:
            cout<<"Wrong Choice!";
        }
    }
    void putdata()
    {
        cout<<"Name of student : "<<name<<endl;
        cout<<"Registration No. : "<<regn<<endl;
        cout<<"Examination Center : "<<examcenter<<endl;
    }
    static void getcount()
    {
        cout<<"Total no.of students in Examination Center\nVadodara : "<<ECV_Cnt<<"\nSurat : "<<E
CS_Cnt<<"\nAhmedabad : "<<ECA_Cnt;
    }
};
int Gate::ECV_Cnt=0;
int Gate::ECS_Cnt=0;
int Gate::ECA_Cnt=0;

int main()
{
    Gate students[5];
    for(int i=0;i<5;i++)
    {
        students[i].getdata();
    }
    cout<<"\n\n";

    for(int i=0;i<5;i++)
    {
        students[i].putdata();
        cout<<"\n";
    }
    Gate::getcount();
    cout << "\n\n                                                             -- Name : Malav Patel\n                                     
                           ID   : 20DIT059\n\n";
    return 0;
}
OUTPUT :

22
Object Oriented Programming with C++ [CE144]
ID No.: 20DIT059

CONCLUSION: In this practical we learned about Static member function and static data
member.

Que 18: Define a class Fahrenheit with float temp as data member. Define another class
Celsius with float temperature as data member. Both classes have member functions to input
and print data. Write a non-member function that receives objects of both the classes and
declare which one is higher than another according to their values. Also define main() to test the
function. Define all member functions outside the class. (Formula for converting Celsius to
Fahrenheit is F = (9C/5) + 32). Use the concept of friend function.
PROGRAM CODE:
#include<iostream>
using namespace std;
class Fahrenheit;
class Celsius
{
public:
    float temperature;
    void input()
    {
        cout<<"Enter temperature in Celsius : ";
        cin>>temperature;
    }
    void display()
    {

23
Object Oriented Programming with C++ [CE144]
ID No.: 20DIT059

        cout<<"Temperature in C : "<<temperature<<endl;
    }
    friend void compare(Fahrenheit f, Celsius c);
};
class Fahrenheit
{
public:
    float temp;
    void input()
    {
        cout<<"Enter temperature in Fahrenheit : ";
        cin>>temp;
    }
    void display()
    {
        cout<<"Temperature in F : "<<temp<<endl;
    }
    friend void compare(Fahrenheit , Celsius );
};
void compare(Fahrenheit f, Celsius c)
{
    float ftemp;
    ftemp = ((9.0*c.temperature)/5.0)+32;
    if(f.temp > ftemp)
        cout<<"Fahrenheit has higher temperature.";
    else if(f.temp < ftemp)
        cout<<"Celsius has higher temperature.";
    else
        cout<<"Fahrenheit and Celsius have same temperature.";
}
int main()
{
    Fahrenheit ft;
    Celsius ct;
    ft.input();
    ct.input();
    ft.display();
    ct.display();
    compare(ft,ct);
    cout << "\n\n                                                             -- Name : Malav Patel\n                                     
                           ID   : 20DIT059\n\n";

    return 0;
}
OUTPUT :

24
Object Oriented Programming with C++ [CE144]
ID No.: 20DIT059

CONCLUSION: In this practical we learned how to make friend functions for different
classes.

25

You might also like