0% found this document useful (0 votes)
150 views38 pages

Subjet 258

The document is a lab report on object oriented programming using C++. It contains code examples and explanations of various OOP concepts in C++ like function overloading, inline functions, friend functions, constructors (default, parameterized, copy), static data members, operator overloading, inheritance (single, multiple, multilevel, virtual, hierarchical), virtual functions, pure virtual functions, file input/output functions like put() and gets(), read() and write(). The document is submitted by a student for their BCA 3rd year lab assignment on C++ with their details and a reference number.

Uploaded by

Abhishek
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
150 views38 pages

Subjet 258

The document is a lab report on object oriented programming using C++. It contains code examples and explanations of various OOP concepts in C++ like function overloading, inline functions, friend functions, constructors (default, parameterized, copy), static data members, operator overloading, inheritance (single, multiple, multilevel, virtual, hierarchical), virtual functions, pure virtual functions, file input/output functions like put() and gets(), read() and write(). The document is submitted by a student for their BCA 3rd year lab assignment on C++ with their details and a reference number.

Uploaded by

Abhishek
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

Subject Code: BCA 307

Lab: Object Oriented Programming using C++

Submitted by: ABHISHEK

University Roll Number: 8021823

Class: BCA 3rd year DDE

Reference Number: 19233015


BCA 3rd year Lab C++ Reference Number:19233015

Contents
Function Overloading ..................................................................................................................3
Inline Functions ..........................................................................................................................4
Friend Function ...........................................................................................................................7
Default Constructors ...................................................................................................................9
Parameterized Constructors ....................................................................................................... 11
Copy Constructor ...................................................................................................................... 13
Static data members .................................................................................................................. 15
Overloading unary operator ....................................................................................................... 17
Overloading Binary Operator .................................................................................................... 18
Single Inheritance ..................................................................................................................... 20
Multiple Inheritance .................................................................................................................. 21
Multilevel Inheritance ............................................................................................................... 23
Hybrid (Virtual) Inheritance ...................................................................................................... 25
Hierarchical Inheritance ............................................................................................................ 27
Virtual Function ........................................................................................................................ 29
Pure Virtual Functions .............................................................................................................. 31
put() and gets() function ............................................................................................................ 32
Using the put() function to perform the file output operation. ........................................... 32
read() and write() Functions ...................................................................................................... 35

2
BCA 3rd year Lab C++ Reference Number:19233015

Function Overloading

#include <iostream>

using namespace std;

void print(int i) {

cout << " Here is int " << i << endl;

void print(double f) {

cout << " Here is float " << f << endl;

void print(char const *c) {

cout << " Here is char* " << c << endl;

int main() {

print(10);

print(10.10);

print("ten");

return 0;

Output:
Here is int 10
Here is float 10.1
Here is char* ten

3
BCA 3rd year Lab C++ Reference Number:19233015

Inline Functions
#include <iostream>

using namespace std;

class operation

int a,b,add,sub,mul;

float div;

public:

void get();

void sum();

void difference();

void product();

void division();

};

inline void operation :: get()

cout << "Enter first value:";

cin >> a;

cout << "Enter second value:";

cin >> b;

inline void operation :: sum()

add = a+b;

cout << "Addition of two numbers: " << a+b << "\n";

4
BCA 3rd year Lab C++ Reference Number:19233015

inline void operation :: difference()

sub = a-b;

cout << "Difference of two numbers: " << a-b << "\n";

inline void operation :: product()

mul = a*b;

cout << "Product of two numbers: " << a*b << "\n";

inline void operation ::division()

div=a/b;

cout<<"Division of two numbers: "<<a/b<<"\n" ;

int main()

cout << "Program using inline function\n";

operation s;

s.get();

s.sum();

5
BCA 3rd year Lab C++ Reference Number:19233015

s.difference();

s.product();

s.division();

return 0;

Output:
Enter first value: 45
Enter second value: 15
Addition of two numbers: 60
Difference of two numbers: 30
Product of two numbers: 675
Division of two numbers: 3

6
BCA 3rd year Lab C++ Reference Number:19233015

Friend Function
#include <iostream>

class B;

class A {

public:

void showB(B&);

};

class B {

private:

int b;

public:

B() { b = 0; }

friend void A::showB(B& x); // Friend function

};

void A::showB(B& x)

// Since showB() is friend of B, it can

// access private members of B

std::cout << "B::b = " << x.b;

7
BCA 3rd year Lab C++ Reference Number:19233015

int main()

A a;

B x;

a.showB(x);

return 0;

Output:
B::b = 0

8
BCA 3rd year Lab C++ Reference Number:19233015

Default Constructors
// Cpp program to illustrate the

// concept of Constructors

#include <iostream>

using namespace std;

class construct {

public:

int a, b;

// Default Constructor

construct()

a = 10;

b = 20;

};

int main()

// Default constructor called automatically

// when the object is created

construct c;

cout << "a: " << c.a << endl

<< "b: " << c.b;

return 1;

9
BCA 3rd year Lab C++ Reference Number:19233015

Output:
a: 10
b: 20

10
BCA 3rd year Lab C++ Reference Number:19233015

Parameterized Constructors
// CPP program to illustrate

// parameterized constructors

#include <iostream>

using namespace std;

class Point {

private:

int x, y;

public:

// Parameterized Constructor

Point(int x1, int y1)

x = x1;

y = y1;

int getX()

return x;

int getY()

return y;

11
BCA 3rd year Lab C++ Reference Number:19233015

};

int main()

// Constructor called

Point p1(10, 15);

// Access values assigned by constructor

cout << "p1.x = " << p1.getX() << ", p1.y = " << p1.getY();

return 0;

Output:
p1.x = 10, p1.y = 15

12
BCA 3rd year Lab C++ Reference Number:19233015

Copy Constructor
#include<iostream>

using namespace std;

class Point

private:

int x, y;

public:

Point(int x1, int y1) { x = x1; y = y1; }

// Copy constructor

Point(const Point &p2) {x = p2.x; y = p2.y; }

int getX() { return x; }

int getY() { return y; }

};

int main()

Point p1(10, 15); // Normal constructor is called here

Point p2 = p1; // Copy constructor is called here

// Let us access values assigned by constructors

cout << "p1.x = " << p1.getX() << ", p1.y = " << p1.getY();

cout << "\np2.x = " << p2.getX() << ", p2.y = " << p2.getY();

13
BCA 3rd year Lab C++ Reference Number:19233015

return 0;

Output:
p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15

14
BCA 3rd year Lab C++ Reference Number:19233015

Static data members


#include <iostream>

using namespace std;

class A

int x;

public:

A() { cout << "A's constructor called " << endl; }

};

class B

static A a;

public:

B() { cout << "B's constructor called " << endl; }

static A getA() { return a; }

};

A B::a; // definition of a

int main()

B b1, b2, b3;

A a = b1.getA();

15
BCA 3rd year Lab C++ Reference Number:19233015

return 0;

Output:
A's constructor called
B's constructor called
B's constructor called
B's constructor called

16
BCA 3rd year Lab C++ Reference Number:19233015

Overloading unary operator


// C++ program to show unary operator overloading
#include <iostream>

using namespace std;

class Distance {
public:

// Member Object
int feet, inch;

// Constructor to initialize the object's value


Distance(int f, int i)
{
this->feet = f;
this->inch = i;
}

// Overloading(-) operator to perform decrement


// operation of Distance object
void operator-()
{
feet--;
inch--;
cout << "\nFeet & Inches(Decrement): " << feet << "'" << inch;
}
};

// Driver Code
int main()
{
// Declare and Initialize the constructor
Distance d1(8, 9);
// Use (-) unary operator by single operand
-d1;
return 0;
}

Output:
Feet & Inches(Decrement): 7'8

17
BCA 3rd year Lab C++ Reference Number:19233015

Overloading Binary Operator


// C++ program to show binary operator overloading
#include <iostream>

using namespace std;

class Distance {
public:
// Member Object
int feet, inch;
// No Parameter Constructor
Distance()
{
this->feet = 0;
this->inch = 0;
}

// Constructor to initialize the object's value


// Parametrized Constructor
Distance(int f, int i)
{
this->feet = f;
this->inch = i;
}

// Overloading (+) operator to perform addition of


// two distance object
Distance operator+(Distance& d2) // Call by reference
{
// Create an object to return
Distance d3;

// Perform addition of feet and inches


d3.feet = this->feet + d2.feet;
d3.inch = this->inch + d2.inch;

// Return the resulting object


return d3;
}
};

18
BCA 3rd year Lab C++ Reference Number:19233015

// Driver Code
int main()
{
// Declaring and Initializing first object
Distance d1(8, 9);

// Declaring and Initializing second object


Distance d2(10, 2);

// Declaring third object


Distance d3;

// Use overloaded operator


d3 = d1 + d2;

// Display the result


cout << "\nTotal Feet & Inches: " << d3.feet << "'" << d3.inch;
return 0;
}
Output:
Total Feet & Inches: 18'11

19
BCA 3rd year Lab C++ Reference Number:19233015

Single Inheritance
// C++ program to explain

// Single inheritance

#include <iostream>

using namespace std;

// base class

class Vehicle {

public:

Vehicle()

cout << "This is a Vehicle" << endl;

};

// sub class derived from two 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

20
BCA 3rd year Lab C++ Reference Number:19233015

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" << endl;

};

// second base class

class FourWheeler {

public:

FourWheeler()

cout << "This is a 4 wheeler Vehicle" << endl;

};

// sub class derived from two base classes

class Car: public Vehicle, public FourWheeler {

21
BCA 3rd year Lab C++ Reference Number:19233015

};

// 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

22
BCA 3rd year Lab C++ Reference Number:19233015

Multilevel Inheritance
// C++ program to implement

// Multilevel Inheritance

#include <iostream>

using namespace std;

// base class

class Vehicle

public:

Vehicle()

cout << "This is a Vehicle" << endl;

};

class fourWheeler: public Vehicle

{ public:

fourWheeler()

cout<<"Objects with 4 wheels are vehicles"<<endl;

};

// sub class derived from two base classes

class Car: public fourWheeler{

public:

car()

23
BCA 3rd year Lab C++ Reference Number:19233015

cout<<"Car has 4 Wheels"<<endl;

};

// 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

24
BCA 3rd year Lab C++ Reference Number:19233015

Hybrid (Virtual) Inheritance


// C++ program for Hybrid Inheritance

#include <iostream>

using namespace std;

// base class

class Vehicle

public:

Vehicle()

cout << "This is a Vehicle" << endl;

};

//base class

class Fare

public:

Fare()

cout<<"Fare of Vehicle\n";

};

25
BCA 3rd year Lab C++ Reference Number:19233015

// first sub class

class Car: public Vehicle

};

// second sub class

class Bus: public Vehicle, public Fare

};

// main function

int main()

// creating object of sub class will

// invoke the constructor of base class

Bus obj2;

return 0;

Output:
This is a Vehicle
Fare of Vehicle

26
BCA 3rd year Lab C++ Reference Number:19233015

Hierarchical Inheritance
// C++ program to implement

// Hierarchical Inheritance

#include <iostream>

using namespace std;

// base class

class Vehicle

public:

Vehicle()

cout << "This is a Vehicle" << endl;

};

// first sub class

class Car: public Vehicle

};

// second sub class

class Bus: public Vehicle

27
BCA 3rd year Lab C++ Reference Number:19233015

};

// main function

int main()

// creating object of sub class will

// invoke the constructor of base class

Car obj1;

Bus obj2;

return 0;

Output:
This is a Vehicle
This is a Vehicle

28
BCA 3rd year Lab C++ Reference Number:19233015

Virtual Function
// CPP program to illustrate

// working of Virtual Functions

#include <iostream>

using namespace std;

class base {

public:

void fun_1() { cout << "base-1\n"; }

virtual void fun_2() { cout << "base-2\n"; }

virtual void fun_3() { cout << "base-3\n"; }

virtual void fun_4() { cout << "base-4\n"; }

};

class derived : public base {

public:

void fun_1() { cout << "derived-1\n"; }

void fun_2() { cout << "derived-2\n"; }

void fun_4(int x) { cout << "derived-4\n"; }

};

int main()

base* p;

derived obj1;

p = &obj1;

29
BCA 3rd year Lab C++ Reference Number:19233015

// Early binding because fun1() is non-virtual

// in base

p->fun_1();

// Late binding (RTP)

p->fun_2();

// Late binding (RTP)

p->fun_3();

// Late binding (RTP)

p->fun_4();

// Early binding but this function call is

// illegal(produces error) becasue pointer

// is of base type and function is of

// derived class

// p->fun_4(5);

Output:
base-1
derived-2
base-3
base-4

30
BCA 3rd year Lab C++ Reference Number:19233015

Pure Virtual Functions


#include<iostream>

using namespace std;

class Base

int x;

public:

virtual void fun() = 0;

int getX() { return x; }

};

// This class inherits from Base and implements fun()

class Derived: public Base

int y;

public:

void fun() { cout << "fun() called"; }

};

int main(void)

Derived d;

d.fun();

return 0;

Output:
fun() called

31
BCA 3rd year Lab C++ Reference Number:19233015

put() and gets() function


Using the put() function to perform the file output operation.

//Writing data to a file using ofstream class and mode ios::out

#include<iostream>
#include<fstream>
#include<cstring>

using namespace std;

int main()
{

//Creating an output stream to write data to a file


ofstream ofstream_ob;

//Opening a file named File2.txt to modify the old content


ofstream_ob.open("File2.txt", ios::out);

char arr[100] = "Hello World. We wish you best in everything. Never give up!";

int length = strlen(arr);


char ch;

//Reading the char array i.e. a character at a time and writing it to the file
for(int i=0; i<length; i++)
{
ch = arr[i];

32
BCA 3rd year Lab C++ Reference Number:19233015

ofstream_ob.put(ch); //Writing a character to a file using put() function


}

//Closing the output stream


ofstream_ob.close();

return 0;
}
Output

Executing this program will create a new file named File2.txt in the current directory and we
have even written the content of this file using put function. The content of the file looks like this
:

File2.txt
Hello World. We wish you best in everything. Never give up!

33
BCA 3rd year Lab C++ Reference Number:19233015

gets()

#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
char str[100];
cout << "Enter a string: ";
gets(str);
cout << "You entered: " << str;

return 0;
}
Output:

Enter a string: Have a great day!


You entered: Have a great day!

34
BCA 3rd year Lab C++ Reference Number:19233015

read() and write() Functions

//Writing a class object to a file using ofstream class and mode ios::out

#include<iostream>
#include<fstream>

using namespace std;

class A
{
private:
char name[40];
int age;
float height;
char gender;

public:
void putdata();
void getdata();
};
//Defining the function putdata() to enter the values of data members of an object.
void A :: putdata()
{
cout<<"Enter the name : ";
cin.getline(name,40);
cout<<"Enter the age : ";
cin>>age;

35
BCA 3rd year Lab C++ Reference Number:19233015

cout<<"Enter the height : ";


cin>>height;
cout<<"Enter the gender : ";
cin>>gender;
}
//Defining the function getdata() to read the values of data members of an object.
void A :: getdata()
{
cout<<"The name is : " << name << "\n";
cout<<"The age is : " << age << "\n";
cout<<"The height is : " << height << "\n";
cout<<"The gender is : " << gender << "\n";
}
int main()
{
//Creating an output stream
ofstream ofstream_ob;
//Calling the open function to write an object to a file
ofstream_ob.open("File9.txt", ios::out);

//Creating an object of A class


A ob1;
//Calling the putdata() function
ob1.putdata();
//Calling the write() function to write an object to a file.
ofstream_ob.write( (char *) & ob1, sizeof(ob1));
cout<<"Congrats! Your object is successfully written to the file \n";

36
BCA 3rd year Lab C++ Reference Number:19233015

//Closing the output stream


ofstream_ob.close();
//Creating an input stream
ifstream ifstream_ob;
//Calling the open function to read an object from a file
ifstream_ob.open("File11.txt", ios::in);
//Creating an empty object of A class
A ob2;
cout<<"\nReading the object from a file : \n";
//Calling the read() function to read an object from a file and transfer its content to an empty
object
ifstream_ob.read( (char *) & ob2, sizeof(ob2));
//Calling the getdata() function
ob2.getdata();
//Closing the input stream
ifstream_ob.close();
return 0;
}

37
BCA 3rd year Lab C++ Reference Number:19233015

Output
Enter the name : Arthur
Enter the age : 29
Enter the height : 1.78
Enter the gender : M

Congrats! Your object is successfully written to the file

Reading the object from a file :


The name is : Arthur
The age is : 29
The height is : 1.78
The gender is : M

38

You might also like