Subject Code 307
Subject Code 307
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;
}
4
BCA 3rd year Lab C++ Reference Number:19233015
int main()
{
cout << "Program using inline function\n";
operation s;
s.get();
s.sum();
s.difference();
s.product();
s.division();
return 0;
5
BCA 3rd year Lab C++ Reference Number:19233015
}
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;
}
int main()
{
7
BCA 3rd year Lab C++ Reference Number:19233015
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);
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 main()
{
Point p1(10, 15); // Normal constructor is called here
Point p2 = p1; // Copy constructor is called here
return 0;
13
BCA 3rd year Lab C++ Reference Number:19233015
}
Output:
p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
14
BCA 3rd year Lab C++ Reference Number:19233015
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();
return 0;
}
15
BCA 3rd year Lab C++ Reference Number:19233015
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
class Distance {
public:
// Member Object
int 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
class Distance {
public:
// Member Object
int feet, inch;
// No Parameter Constructor
Distance()
{
this->feet = 0;
this->inch = 0;
}
// Driver Code
int main()
{
// Declaring and Initializing first object
Distance d1(8, 9);
18
BCA 3rd year Lab C++ Reference Number:19233015
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
Multiple Inheritance
// C++ program to explain
20
BCA 3rd year Lab C++ Reference Number:19233015
// multiple inheritance
#include <iostream>
using namespace std;
};
// main function
21
BCA 3rd year Lab C++ Reference Number:19233015
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()
{
cout<<"Car has 4 Wheels"<<endl;
23
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
Objects with 4 wheels are vehicles
Car has 4 Wheels
24
BCA 3rd year Lab C++ Reference Number:19233015
#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
};
};
// 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;
}
};
};
};
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"; }
};
int main()
{
base* p;
derived obj1;
p = &obj1;
29
BCA 3rd year Lab C++ Reference Number:19233015
// in base
p->fun_1();
30
BCA 3rd year Lab C++ Reference Number:19233015
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
#include<iostream>
#include<fstream>
#include<cstring>
int main()
{
char arr[100] = "Hello World. We wish you best in everything. Never give up!";
//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];
ofstream_ob.put(ch); //Writing a character to a file using put() function
}
32
BCA 3rd year Lab C++ Reference Number:19233015
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>
int main()
{
char str[100];
cout << "Enter a string: ";
gets(str);
cout << "You entered: " << str;
return 0;
}
Output:
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>
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
36
BCA 3rd year Lab C++ Reference Number:19233015
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
38