Professional Documents
Culture Documents
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>
void print(int i) {
void print(double f) {
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>
class operation
int a,b,add,sub,mul;
float div;
public:
void get();
void sum();
void difference();
void product();
void division();
};
cin >> a;
cin >> b;
add = a+b;
cout << "Addition of two numbers: " << a+b << "\n";
4
BCA 3rd year Lab C++ Reference Number:19233015
sub = a-b;
cout << "Difference of two numbers: " << a-b << "\n";
mul = a*b;
cout << "Product of two numbers: " << a*b << "\n";
div=a/b;
int main()
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; }
};
void A::showB(B& x)
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>
class construct {
public:
int a, b;
// Default Constructor
construct()
a = 10;
b = 20;
};
int main()
construct c;
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>
class Point {
private:
int x, y;
public:
// Parameterized Constructor
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
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>
class Point
private:
int x, y;
public:
// Copy constructor
};
int main()
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
class A
int x;
public:
};
class B
static A a;
public:
};
A B::a; // definition of a
int main()
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
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;
}
18
BCA 3rd year Lab C++ Reference Number:19233015
// Driver Code
int main()
{
// Declaring and Initializing first object
Distance d1(8, 9);
19
BCA 3rd year Lab C++ Reference Number:19233015
Single Inheritance
// C++ program to explain
// Single inheritance
#include <iostream>
// base class
class Vehicle {
public:
Vehicle()
};
};
// main function
int main()
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>
class Vehicle {
public:
Vehicle()
};
class FourWheeler {
public:
FourWheeler()
};
21
BCA 3rd year Lab C++ Reference Number:19233015
};
// main function
int main()
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>
// base class
class Vehicle
public:
Vehicle()
};
{ public:
fourWheeler()
};
public:
car()
23
BCA 3rd year Lab C++ Reference Number:19233015
};
// main function
int main()
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>
// base class
class Vehicle
public:
Vehicle()
};
//base class
class Fare
public:
Fare()
cout<<"Fare of Vehicle\n";
};
25
BCA 3rd year Lab C++ Reference Number:19233015
};
};
// main function
int main()
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>
// base class
class Vehicle
public:
Vehicle()
};
};
27
BCA 3rd year Lab C++ Reference Number:19233015
};
// main function
int main()
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
#include <iostream>
class base {
public:
};
public:
};
int main()
base* p;
derived obj1;
p = &obj1;
29
BCA 3rd year Lab C++ Reference Number:19233015
// in base
p->fun_1();
p->fun_2();
p->fun_3();
p->fun_4();
// derived class
// p->fun_4(5);
Output:
base-1
derived-2
base-3
base-4
30
BCA 3rd year Lab C++ Reference Number:19233015
class Base
int x;
public:
};
int y;
public:
};
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];
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
//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