Friday 17 February 2023

Lab Report | C++ |

 

TRIBHUVAN UNIVERSITY

MAHENDRA MORANG ADARSH MULTIPLE CAMPUS

BIRATNAGAR, MORANG

 

A Lab Report on

 

“Object Oriented Programming with C++”

Submitted in partial fulfilment of the requirement for the award of the degree of

BICTE

(ICT. ED.426)

 

 

 

 

Submitted by:

 

NAME:

SYMBOL NO:

 

Under the guidance of

ER. BIBEK KUMAR SARDAR

Department of Information Science and

Engineering

 

Submitted to:

MAHENDRA MORANG ADARSH MULTIPLE CAMPUS

Institute of Science and Technology

Tribhuvan University

2079

 

 

 

INDEX

Lab. No.

Title of the Lab

Date of Lab

Date of Submission

Signature

1

Basic concept of C++ and its simple program

 

 

 

2

Create class and objects with data member and member function

 

 

 

3

Create static function/ Create friend function

 

 

 

4

Create different types of constructors

 

 

 

5

Concept of operator overloading

 

 

 

6

Concept of inheritance (single level/multiple/multilevel inheritance)

 

 

 

7

Create virtual and pure virtual function

 

 

 

8

Create abstract and container class

 

 

 

9

Create function template and class template

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

                                          Lab:1

                                                                                                 Date:

Title: Basic concept of C++ and its simple program

Introduction:

Objects are the run-time entities in object oriented system. Everything in C++ is associated with classes and objects. Along with its attributes and methods. For example: in real life, a car is an object. The car has attributes, such as weight and color, and methods, such as drive and brake.

Attributes and methods are basically variables and functions that belongs to the class. These are often referred to as "class members".

A class is a user-defined data type that we can use in our program, and it works as an object constructor, or a "blueprint" for creating objects.

//write a C++ Program to find Largest among 3 numbers using classes and object.

#include<iostream>
using namespace std;
class greatest
{
        private:
                int x,y,z;
        public:
        void input()
        {
                cout<<"Enter 3 nos.";
                cin>>x>>y>>z;
        }
        void calc()
        {
                int r;
                r=((x>y)&&(x>z)?x:(y>x)&&(y>z)?y:z);
                cout<<"Greatest no:"<<r;
        }
};
int main()
{
        greatest g;
        g.input();
        g.calc();
        
}

 

                                                Lab:2

                                                                                                 Date:

Title: Create class and objects with data member and member function

Introduction:

Data member: 
1.     The variable declared inside class are known as data members.
2.     Data member may be private or public.
      Member function: 
1.     The functions declared inside the class are known as member functions.
2.     Member functions are methods or functions that are defined inside of objects.
3.     Generally used to manipulate data members and other object data.
 

 

Program to create class and objects with data member and member function

#include<iostream>
using namespace std;
class student
{
        private:
                int id;// data member
                char name[20]; // data member 
 
        public:
 
        void getdata(void)//member function
        void display(void)//member function
        {
                cout<<id<<’\t’<<name<<endl;
                
        }
        
};
int main()
{
        student s;
        s.getdata(void);
        s.display(void);
        
}

 

 

 

 

                                            Lab:3

                                                                                                 Date:

Title: Create static function// Create friend function

Introduction:

Static function:

A class may also have static methods as its member. Static function is defined by using the keyword static before the member function that is to be declared as static function.

For example:

Static return_type function_name (argument_list)

    {
        body
        
    }

Friends Functions

 

Friend function is a function which is not the member of the class instead of that it can access private and protected member of class. A friend function can access the private and protected data of a class. We declare a friend function using the friend keyword inside the body of the class.

Syntax:-

class className {

    ... .. ...

    friend returnType functionName(class obj);

    ... .. ...

}

 

Example: write a program to illustrate static member function.

#include<iostream>
using namespace std;
class Box
{
    static int cn;
    public:
    static int Getcount()
    {
        Return count;
 
    }
    Static int IncreaseCount()
    
    {
        Count++;
                
    }
 
};
int Box::count; 
void main()
{
    Cout<<”initially value of count:”<<Box::GetCount()<<endl;
    Box b1,b2, b3;
    Box:: IncreaseCount ();
    Box:: IncreaseCount ();
    Box:: IncreaseCount ();
    Cout<<”finally value of count:”<<Box::Getount()<<endl;
}

Output:

Initially value of count: 0

Finally value of count:3

 

// Example of friend function

 

#include <iostream>

using namespace std;

 

class A

{

    int a, b;

    public:

    void input()

    {

         Cout<<”enter value:”;

         Cin>>a>>b;

    }      

         friend void add(A ob);

};

 

void add(A ob)

{

    int c;

    c=ob.a+ob.c;

    cout<<”sum=<<c;

}

 

int main()

{

    A kk;

    kk.input();

    add(kk);

    return 0;

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

                                            Lab:4

                                                                                                 Date:

Title: Create different types of constructors

Introduction:

Constructors are special class functions which performs initialization of every object. The Compiler calls the Constructor whenever an object is created. Constructors initialize values to object members after storage is allocated to the object.

The syntax of defining a constructor function in a class:

class A
{
    public:
    int x;
    // constructor
    A()
    {
        // object initialization
    }
};

//example of Default constructor

#include<iostream>

using namespace std

class Cube
{
    public:
    int side;
    Cube()
    {
        side = 10;
    }
};
 
int main()
{
    Cube c;
    cout << c.side;
}

10    

 

 

// example of Parameterized Constructors

#include<iostream>

using namespace std

class Cube
{
    public:
    int side;
    Cube(int x)
    {
        side=x;
    }
};
 
int main()
{
    Cube c1(10);
    Cube c2(20);
    Cube c3(30);
    cout << c1.side;
    cout << c2.side;
    cout << c3.side;
}

10   0 30

// example of Copy Constructors

#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;
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

                                           Lab:5

                                                                                                 Date:

Title: Concept of operator overloading

Introduction:

           Unary Operator Overloading

The operator that requires only one operand to perform its operation is known as unary operator. For example: unary minus (-), unary plus (+), increment (++), decrement—, logical not (!), pointer indirection or dereferences (*) and address of ( &).

The operator function declared as member function can be used with following syntax.

Return_type operator unary_operator ()

        {
                Body;
             
        }
 

Binary Operator Overloading

The operator which require two operands for its operation is known as binary operators. For example: binary plus (  +), binary (  -), multiplication (*), division ( /), greater than ( >), less than (<), equality operator (= =) etc. the member function can be used as operator function using syntax

Return_type operator binary_operator (object2_of_class)

        {
                Body;
             
        }
 

//write a program to overload a unary minus operator (i.e. -).

 

#include <iostream>
using namespace std;
 
class complex {
      int real, image;             
           
   public:
      // required constructors
      complex(int r, int i) {
         real = r;
         image = i;
      }
      complex() {
         real = 0;
         img = 0;
      }
      
      
      // overloaded minus (-) operator
      void operator- () {
         real = -real;
         img = -image;
         
      }
      // method to display complex
      void display() {
         cout << "Real Part: " << real <<endl;
         cout << "Imaginary Part: " << img <<endl;
      }
 
};
 
Void main() {
   Complex comp(-7, 3);
   cout << "the complex number is: "<<endl;
   comp.Display();
   -comp; //same as comp.operator –()
   cout << endl<<”the complex number after unary minus:”<<endl;
   comp.Display();
}
 

Output

The complex number is :

Real Part:4

Imaginary part:5

The complex number after unary minus:

Real Part:-4

Imaginary Part:-5

 

 

//write a program to overload a binary plus operator (+) for addition of two complex number.

 

#include <iostream>
using namespace std;
 
class complex {
   private:
      float real, image;             
           
   public:
      // required constructors
      complex() {
         real = 0;
         image = 0;
      }
      complex(float r, float i) {
         real = r;
         img = i;
      }
      
      
      // overloaded binary plus operator +
      Complex operator + (complex c) {
         Complex temp;
         temp.real=real+c.real;
         temp.img=img+c.img;
         return (temp);
                  
      }
      // method to display complex
      void display() {
         cout << real<<”+j”<<img<<endl;
         
      }
 
};
 
Void main() {
   Complex c1 (2, 5),c2 (4, 6), c3;
   C3=c1+c2; //equivalent to c1.operator+(c2)
   Cout<<”c1=”,c1.display();
   Cout<<”c2=”,c2.display();
   Cout<<”c3=”,c3.display();
   
  }
 

Output

C1=2 +j 5

C2=4 +j 6

C3=6 +j 11

 

 

 

       

                                          Lab:6

                                                                                                 Date:

Title: Concept of inheritance (single level/multiple/multilevel inheritance)

Introduction:

Single Inheritance: In single inheritance, a class is allowed to inherit from only one class. i.e. one sub class is inherited by one base class only.

 

Syntax

class subclass_name : access_mode base_class

{

  // body of subclass

};

//Example of single inheritance:

 

// C++ program to explain

// Single inheritance

#include<iostream>

using namespace std;

 

// base class

class Vehicle {

  public:

    Vehicle()

    {

      cout << "This is a Vehicle\n";

    }

};

 

// sub class derived from a single base classes

class Car : public Vehicle {

 

};

 

// main function

int main()

{  

    // Creating object of sub class will

    // invoke the constructor of base classes

    Car obj;

    return 0;

}

Output

This is a Vehicle

2. Multiple Inheritance: Multiple Inheritance is a feature of C++ where a class can inherit from more than one classes. i.e one sub class is inherited from more than one base classes.

 

Syntax

class subclass_name : access_mode base_class1, access_mode base_class2, ....

{

  // body of subclass

};

//Example of multiple inheritance:

// C++ program to explain

// multiple inheritance

#include<iostream>

using namespace std;

 

// first base class

class Vehicle {

  public:

    Vehicle()

    {

      cout << "This is a Vehicle\n";

    }

};

 

// second base class

class FourWheeler {

  public:

    FourWheeler()

    {

      cout << "This is a 4 wheeler Vehicle\n";

    }

};

 

// sub class derived from two base classes

class Car : public Vehicle, public FourWheeler {

 

};

 

// main function

int main()

{  

    // Creating object of sub class will

    // invoke the constructor of base classes.

    Car obj;

    return 0;

}

Output

This is a Vehicle

This is a 4 wheeler Vehicle

3. Multilevel Inheritance: In this type of inheritance, a derived class is created from another derived class.

//Example of muuultilevel inheitance

// C++ program to implement

// Multilevel Inheritance

#include<iostream>

using namespace std;

 

// base class

class Vehicle

{

  public:

    Vehicle()

    {

      cout << "This is a Vehicle\n";

    }

};

 

// first sub_class derived from class vehicle

class fourWheeler: public Vehicle

{  public:

    fourWheeler()

    {

      cout << "Objects with 4 wheels are vehicles\n";

    }

};

// sub class derived from the derived base class fourWheeler

class Car: public fourWheeler {

   public:

     Car()

     {

       cout << "Car has 4 Wheels\n";

     }

};

 

// main function

int main()

{  

    // Creating object of sub class will

    // invoke the constructor of base classes.

    Car obj;

    return 0;

}

Output

This is a Vehicle

Objects with 4 wheels are vehicles

Car has 4 Wheels

 

                                         Lab:7

                                                                                                 Date:

Title: Create virtual and pure virtual function

Introduction:

C++ virtual function

A C++ virtual function is a member function in the base class that you redefine in a derived class. It is declared using the virtual keyword. It is used to tell the compiler to perform dynamic linkage or late binding on the function.

Pure virtual function

 

           A pure virtual function in C++, also known as the do-nothing function, is a virtual function that        does not perform any task. It is only used as a placeholder and does not contain any function definition (do-nothing function). It is declared in an abstract base class. These types of classes cannot declare any objects of their own. We can drive a pure virtual function in C++ using the following syntax:

 

Virtual void class_name()=0;

 

//Example of virtual function:

  1. #include <iostream.h>  
  2. #include <conio.h>  
  3. class Figure
  4. {  
  5. Protected:
  6.    Float dim1,dim2;
  7. public:  
  8.     Figure(float a, float b)
  9.               {
  10.               dim1=a;
  11.               dim2=b;
  12.                }
  13.      Virtual float Area ()
  14.               {
  15.               return 0.0;
  16.              }
  17.  };
  18. class Rectangle: public Figure
  19. {  
  20. public:  
  21.     Rectangle (float length, float breadth) : Figure (length, breadth)
  22.               {
  23.                }
  24.      float Area ()
  25.               {
  26.               return (dim1*dim2);
  27.              }
  28.  };
  29. class triangle: public Figure
  30. {  
  31. public:  
  32.     Triangle (float height, float base) : Figure (height, base)
  33.               {
  34.                }
  35.      float Area ()
  36.               {
  37.               return (dim1*dim2/2);
  38.              }
  39.  };
  40. void main ()  
  41. {  
  42.     Figure*f;  
  43.     Rectangle rect (100.5, 7.0);
  44.    Triangle  tri (10.5, 5.6);
  45. Float area_rect, area_tri;
  46. Clrscr();
  47. f =&rect;
  48. area_rect=f->Area();
  49. f =&tri;
  50. area_tri=f->Area();
  51. cout<<”Area of Rectangle:”<<area_rect<<endl;
  52. cout<<”Area of triangle:”<<area_tri<<endl;
  53. getch();
  54. }  

Output:

Area of Rectangle:703.5
Area of triangle:29.4

 

//Example of pure virtual function:

  1. #include <iostream.h>  
  2. #include <conio.h>  
  3. class Polygon
  4. {  
  5. public:  
  6.     virtual void Draw ( )=0;
  7. };
  8. class Rectangle: public Polygon
  9. {  
  10. public:  
  11.     void Draw ( )
  12.               {
  13.               Cou<<”Drawing a rectangle….”<<endl;
  14.                }
  15.   };
  16. class Triangle: public polygon
  17. {  
  18. public:  
  19.     void Draw ( )
  20.               {
  21.               Cou<<”Drawing a Triangle….”<<endl;
  22.                }
  23.   };
  24. class Pentagon: public polygon
  25. {  
  26. public:  
  27.     void Draw ( )
  28.               {
  29.               Cou<<”Drawing a Pentagon….”<<endl;
  30.                }
  31.   };
  32. void main ()  
  33. {  
  34.     Polygon *p[3];
  35. Rectangle rect;
  36. Triangle tri;
  37. Pentagon pent;
  38. Clrscr();
  39. P[0]=&rect;
  40. P[1]=&tri;
  41. P[2]=&pent;
  42. For (int i=0; i<3; i++)
  43. P[i]->Draw();
  44. getch();
  45. }  

Output:

Drawing a rectangle…… 
Drawing a triangle…… 
Drawing a pentagon…… 

                                          Lab:8

                                                                                                 Date:

Title: Create abstract and container class

Introduction:

Abstract class

 

An abstract class is a class that is designed to be specifically used as a base class. An abstract class contains at least one pure virtual function. We declare a pure virtual function by using a pure specifier (=0) in the declaration of a virtual member function in the class declaration.

Syntax:

Class AB

  1. {  
  2. public:  
  3.     virtual void f( )=0;
  4. };

Container class

 

 A class is said to be a container class which is utilized for the purpose of holding objects in memory or persistent media. The purpose of container class is to hide the topology for the purpose of objects list maintenance in memory.

 

//An example abstract of shape base class with sub-classes (triangle and rectangle) that inherits the shape class.

 

  1. #include <iostream.h>  
  2. #include <conio.h>  
  3. class shape
  4. {  
  5. public:  
  6.     virtual int Area ( )=0;
  7. Void setwidth (int w)
  8.     {  
  9.      width=w;
  10.     }
  11.  
  12.     Void setheight (int h)
  13.     {  
  14.      height=h;
  15.     }
  16.    Protected:
  17.        int width;
  18.        int height;
  19. };
  20. class Rectangle: public shape
  21. {  
  22. public:  
  23.     int Area ( )
  24.               {
  25.               Return (width*height);
  26.                }
  27.   };
  28. class Triangle: public shape
  29. {  
  30. public:  
  31.     int Area ( )
  32.               {
  33.               Return (width*height);
  34.                }
  35.   };
  36. int main ()  
  37. {  
  38.  Rectangle R;
  39. Triangle T;
  40. R.setwidth (5);
  41. R.setheight(10);
  42. T.setwidth(20);
  43. T.setheight(8);
  44. Cout<<”the area of the rectangle is :”<<R.area()<<endl;
  45. Cout<<”the area of the triangle is :”<<T.area()<<endl;
  46.  
  47. }  

//Example of container class:

  1. #include<iostream>
  2. Class first
  3. {  
  4. Public:
  5. Void show f()
  6. {  
  7. Cout<<”hello from first class \n”;
  8. }  
  9. }  ;
  10. Class second
  11. {  
  12. First f;
  13. Public:
  14. Second ( )
  15. {  
  16. f.show();
  17. }  
  18. }  ;
  19. Int main ()
  20. {  
  21. Second s;
  22. }  
  23.  

Output:

hello from first cla

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

                                          Lab:9

                                                                                                 Date:

Title: Create function template and class template

Introduction:

Function template in C++ is a single function template that works with multiple data types simultaneously, but a standard function works only with one set of data types.

C++ Function Template Syntax

1

2

3

4

template<class type>ret-type func-name(parameter list)

{

//body of the function

}

The class template in c++ is like function templates. They are known as generic templates. They define a family of classes in C++. 

Syntax of Class Template

1

2

3

4

5

template<class Ttype>

class class_name

{

//class body;

}

 

//C++ function template example:

#include<iostream.h>

using namespace std;

template<classX>

X func(Xa,Xb)

{

return a;

}

int main()

cout<<func(15,8),,endl;

cout,,func('p','q'),,endl;

cout<<func(7.5,9.2),,endl;

return();

}

Output:

15
p
7.5

 

 

 

//Class template in c++ example:

#include<iostream.h>

using namespace std;

template <class C>

class A{

private;

C,a,b;

public:

A(Cx,Cy){

a=x;

b=y;

}

void show()

}

count<<"The Addition of"<<a<<"and"<<b<<"is"<<add()<<endl;

}

C add(){

C c=a+b;

return c;

}

};

int main(){

Aaddint(8,6);

Aaddfloat(3.5,2.6);

Aaaddouble(2.156,5.234);

Aaddint.show();

cout<<endl;

adddouble.show();

count<<endl;

return 0;

}

 

Output:

The addition of 8 and 6 is 14
Addition of 3.5 and 2.6 is 6.1
The addition of 2.156 and 5.234 is 7.390

 

 

 

 

 

 

 

 

 

1.      /* write a program to find marks of 10 students in ascending order using array in C */

 

 

 

 

 

 

No comments:

Post a Comment