GCUF BSSE 2nd
GCUF BSSE 2nd
Practical Notebook
Prepared By:
Student Name: ali
Subject
Programming Fundamentals CSI-301 4(3-1)
2023
2
Certificate
Supervisor
Saeed Rasheed Signature
Acknowledgement
I carried the work reported in this Practical Notebook under the
supervision of the Supervisor Mr. Talha Rasheed, at Divisional Public
School and College Faisalabad. I as a result of this declare that this
notebook and the contents of the practical notebook are the product of
my practical implementation and no part has been copied from any
other written or published source (except the references, standard
mathematical or genetics models / equation / formulas / protocol and
recommended book etc.).
Contents
Basics of Object Oriented Programming ....................................................................................................... 6
Object Oriented Programming: ................................................................................................................. 6
Program 13.1 ............................................................................................................................................. 6
Program 13.2 ............................................................................................................................................. 7
Program 13.3 ............................................................................................................................................. 8
Program 13.4 ............................................................................................................................................. 9
Constructor: ............................................................................................................................................ 11
Program 13.7 ........................................................................................................................................... 11
Program 13.8 ........................................................................................................................................... 12
Constructor Overloading: ........................................................................................................................ 13
Program 13.11 ......................................................................................................................................... 13
Default Copy Constructor:....................................................................................................................... 14
Program 13.12 ......................................................................................................................................... 14
Destructors:............................................................................................................................................. 16
Program 13.13 ......................................................................................................................................... 16
Static Data Member: ............................................................................................................................... 17
Program 13.15 ......................................................................................................................................... 17
Program 13.16 ......................................................................................................................................... 18
Friend Function: ...................................................................................................................................... 19
Program 13.17 ......................................................................................................................................... 19
Static Function: ....................................................................................................................................... 21
Program 13.18 ......................................................................................................................................... 21
Operator Overloading ................................................................................................................................. 23
Operator Overloading: ............................................................................................................................ 23
Overloading ++ Operator: ....................................................................................................................... 23
Program 14.1 ........................................................................................................................................... 23
Program 14.2 ........................................................................................................................................... 24
Program 14.3 ........................................................................................................................................... 25
Arithmetic Operators: ............................................................................................................................. 26
Program 14.4 ........................................................................................................................................... 26
Program 14.5 ........................................................................................................................................... 28
5
Inheritance .................................................................................................................................................. 30
Inheritance: ............................................................................................................................................. 30
15.1.1 Advantages of Inheritance ........................................................................................................... 30
1.Reusability ............................................................................................................................................ 30
2.Saves Time and Effort ........................................................................................................................... 30
3.Increases Program Structure and Reliability ........................................................................................ 30
Categories of Inheritance ........................................................................................................................ 30
1. Single Inheritance............................................................................................................................... 30
2. Multiple Inheritance........................................................................................................................... 31
Specifying a Derived Class ....................................................................................................................... 31
Syntax .................................................................................................................................................. 31
Program 15.1 ........................................................................................................................................... 32
Program 15.2 ........................................................................................................................................... 34
Program 15.3 ........................................................................................................................................... 36
POLYMORPHISM AND VIRTUAL................................................................................................................... 38
FUNCTIONS ................................................................................................................................................. 38
Polymorphism ......................................................................................................................................... 38
Program 16.1 .......................................................................................................................................... 38
Pointer to objects .................................................................................................................................... 39
Program 16.2 .......................................................................................................................................... 39
Array of pointers to objects .................................................................................................................... 40
Pointers and Inheritance ......................................................................................................................... 40
Virtual Functions ..................................................................................................................................... 41
Example ............................................................................................................................................... 41
Early Binding ........................................................................................................................................... 42
Late Binding............................................................................................................................................. 42
Pure Virtual Functions............................................................................................................................. 42
Abstract Classes ...................................................................................................................................... 43
Syntax .................................................................................................................................................. 43
6
Program 13.1
Write a program that declares a class with one integer data member
and two member functions in() and out() to input and output data in data
member.
#include<iostream>
#include<conio.h>
using namespace std;
class test
{
private:
int n;
public:
void in()
{
cout<<"Enter Number: ";
cin>>n;
}
void out()
{
cout<<"The value of n = "<<n;
7
}
};
int main()
{
test obj;
obj.in();
obj.out();
}
Program 13.2
Write class marks with three data members to store three marks. Write
three member functions in() to input marks, sum() to calculate and return
the sum and avg() to calculate and return the average marks.
#include<iostream>
#include<conio.h>
using namespace std;
class marks
{
private:
int a,b,c;
public:
void in()
{
cout<<"Enter three number: ";
cin>>a>>b>>c;
}
int sum()
{
return a+b+c;
}
float avg()
{
return (a+b+c)/3.0;
8
}
};
int main()
{
marks m;
int s;
float a;
m.in();
s=m.sum();
a=m.avg();
cout<<"Sum ="<<s<<endl;
cout<<"Average ="<<a;
Program 13.3
Write a class circle with one data member radius. Write three member
functions get() radius to set radius value with parameter value, area() to
display radius and circum() to calculate and display circumference of
circle.
#include<iostream>
#include<conio.h>
using namespace std;
class circle
{
private:
float radius;
public:
void get_radius(float r)
{
radius=r;
}
void area()
9
{
cout<<"Area of circle: "<<3.14*radius*radius;
}
void circum()
{
cout<<"\nCircumference of circle: "<<2*3.14*radius;
}
};
int main()
{
circle c1;
float rad;
cout<<"Enter radius: ";
cin>>rad;
c1.get_radius(rad);
c1.area();
c1.circum();
}
Program 13.4
Write a class book with three data members BookID, Pages and
Price. It also contains the following member functions:
• The get() function is used to input values
• The show() function is used to display values
• The set() function is used to set values of data members using
parameters
• The getPrice() function is used to return values of price
The program should create two object of class and input values for these
objects. The program displays the details of most costly book.
#include<iostream>
#include<conio.h>
using namespace std;
class book
10
{
private:
int bookid,pages;
float price;
public:
void get()
{
cout<<"Enter book id: ";
cin>>bookid;
cout<<"Enter Pages: ";
cin>>pages;
cout<<"Enter Price: ";
cin>>price;
}
void show()
{
cout<<"Bookid ="<<bookid<<endl;
cout<<"Pages ="<<pages<<endl;
cout<<"Price ="<<price<<endl;
}
void set(int id,int pg,float pr)
{
bookid=id;
pages=pg;
price=pr;
}
float getprice()
{
return price;
}
};
int main()
{
book b1,b2;
b1.get();
11
b2.set(2,320,150.75);
cout<<"\nThe detail of most costly book is as follows: "<<endl;
if(b1.getprice()>b2.getprice())
b1.show();
else
b2.show();
}
Constructor:
A constructor is a special method or function that is automatically
called when an object is created. Its main purpose is to initialize the
object's attributes or properties and set up the object's initial state.
Constructors typically have the same name as the class to which they
belong.
Program 13.7
Write a class that displays a simple message on a screen whenever an
object of that class is created.
#include<iostream>
#include<conio.h>
using namespace std;
class Hello
{
private:
int n;
public:
Hello()
{
cout<<"Object created..."<<endl;
}
};
int main()
{
12
Hello x,y,z;
}
Program 13.8
Write a class that contains two integer data members which are
initialized to 100 when object is created. It has a member function avg
that displays average of data members.
#include<iostream>
#include<conio.h>
using namespace std;
class Number
{
private:
int x,y;
public:
Number()
{
x=y=100;
}
void avg()
{
cout<<"x = "<<x<<endl;
cout<<"y = "<<y<<endl;
cout<<"Average = "<<(x+y)/2<<endl;
}
};
int main()
{
Number n;
n.avg();
}
13
Constructor Overloading:
The process declaring multiple constructor with same name but
different parameters is known as constructor overloading. The constructor
with same name must differ in one of the following ways:
• Number of parameters
• Type of parameters
• Sequence of parameters
Program 13.11
Write a class that has num and ch as data members. A constructor
with no parameter initializes num to 0 and ch to ‘x’. A constructor with
two parameters initializes data members with given values and member
function shows displays the values of data members.
#include<iostream>
#include<conio.h>
using namespace std;
class Over
{
private:
int num;
char ch;
public:
Over()
{
num=0;
ch='x';
}
Over(int n,char c)
{
num=n;
ch=c;
}
void show()
14
{
cout<<"num = "<<num<<endl;
cout<<"ch = "<<ch<<endl;
}
};
int main()
{
Over first,second(100,'p');
cout<<"The contents of first:"<<endl;
first.show();
cout<<"The contents of second:"<<endl;
second.show();
}
Program 13.12
Write a class book that has attributes for pages, price and title. It has
two functions to input the values and displays the values. Create three
objects of class and input the values.
#include<iostream>
#include<conio.h>
using namespace std;
class Book
{
private:
int pg,pr;
15
char title[50];
public:
void get()
{
cout<<"Enter title: ";
gets(title);
cout<<"Enter pages: ";
cin>>pg;
cout<<"Enter price: ";
cin>>pr;
}
void show()
{
cout<<"Title: "<<title<<endl;
cout<<"Pages: "<<pg<<endl;
cout<<"Price: "<<pr<<endl;
}
};
int main()
{
Book b1;
b1.get();
Book b2(b1);
Book b3=b1;
cout<<"\nThe detail of b1:"<<endl;
b1.show();
cout<<"\nThe detail of b2:"<<endl;
b2.show();
cout<<"\nThe detail of b3:"<<endl;
b3.show();
}
16
Destructors:
A type of member function that is automatically executed when an
object of that class is destroyed is known as destructor. A destructor has
no return type and its name is same as class name. The destructor is
preceded by tilde sign ~.
Program 13.13
Write a class that displays a simple message on a screen whenever an
object of that class is created and destroyed.
#include<iostream>
#include<conio.h>
using namespace std;
class test
{
private:
int n;
public:
test()
{
cout<<"Object created..."<<endl;
}
~test()
{
cout<<"Object destoryed..."<<endl;
}
};
int main()
{
test a,b;
}
17
Program 13.15
Write a program that counts number of objects created of a particular
class.
#include<iostream>
#include<conio.h>
using namespace std;
class yahoo
{
private:
static int n;
public:
yahoo()
{
n++;
}
void show()
{
cout<<"You have created "<<n<<" object so far."<<endl;
}
};
int yahoo::n=0;
int main()
{
yahoo x,y;
x.show();
yahoo z;
z.show();
}
18
Program 13.16
Write a program that creates three objects of class student. Each
object of class must be assigned a unique roll number. (Hint: Use static
data member for unique roll number.)
#include<iostream>
#include<conio.h>
using namespace std;
class student
{
private:
static int r;
int rno,marks;
char name[1000];
public:
student()
{
r++;
rno=r;
}
void in()
{
cout<<"Enter name:";
cin>>name;
cout<<"Enter marks:";
cin>>marks;
}
void show()
{
cout<<"Roll No:"<<rno<<endl;
cout<<"Name:"<<name<<endl;
cout<<"Marks:"<<marks<<endl;
}
19
};
int student::r=0;
int main()
{
student s1,s2,s3;
s1.in();
s2.in();
s3.in();
cout<<endl;
s1.show();
s2.show();
s3.show();
}
Friend Function:
A type of function that is allowed to access private and protected
members of a particular class from outside the class is called friend
function. A function that is declared in the class with friend keyword
becomes the friend function of the class.
Program 13.17
Write a program in which a friend function accepts two objects of
different classes as parameters. It has to process the private data members
of these classes and then displays the result.
#include<iostream>
#include<conio.h>
using namespace std;
class B;
class A
{
private:
int a;
public:
20
A()
{
a=10;
}
friend void show(A,B);
};
class B
{
private:
int b;
public:
B()
{
b=20;
}
friend void show(A,B);
};
void show(A x,B y)
{
int r;
r=x.a+y.b;
cout<<"value of class A object= "<<x.a<<endl;
cout<<"value of class B object= "<<y.b<<endl;
cout<<"sum of both values= "<<r<<endl;
}
int main()
{
A obj1;
B obj2;
show(obj1,obj2);
}
21
Static Function:
A type of member function that can be accessed without any object
of the class is called static function.
Program 13.18
Write a program that counts the number of objects created of a
particular class. The program must be able to display the result even if no
object is created so far.
#include<iostream>
#include<conio.h>
using namespace std;
class yahoo
{
private:
static int n;
public:
yahoo()
{
n++;
}
static void show()
{
cout<<"you have created "<<n<<" object so far."<<endl;
}
};
int yahoo::n=0;
int main()
{
yahoo::show();
yahoo x,y;
x.show();
yahoo z;
x.show();
22
}
23
Operator Overloading
Operator Overloading:
The process of defining additional meanings of operators is
known as operator overloading. It enables operators to perform different
operations depending on the type of operands. It also enables the operators
to process the user defined data types.
Overloading ++ Operator:
The increment operator ++ is unary operator. It works with single
operand. It increases the value of operand by 1. It only works with
numerical values by default.
Program 14.1
Write a program that overloads increment operator to work with user
defined objects.
#include<iostream>
#include<conio.h>
using namespace std;
class count
{
private:
int n;
public:
count()
{
n=0;
}
void show()
{
cout<<"n = "<<n<<endl;
}
24
void operator++()
{
n++;
}
};
int main()
{
count obj;
obj.show();
++obj;
obj.show();
}
Program 14.2
Write a program that overloads increment operator to work with user
defined objects. The overloaded function should return an object after
incrementing the data member.
#include<iostream>
#include<conio.h>
using namespace std;
class count
{
private:
int n;
public:
count()
{
n=0;
}
void show()
{
cout<<"n = "<<n<<endl;
}
25
count operator++()
{
count temp;
n=n+1;
temp.n=n;
return temp;
}
};
int main()
{
count x,y;
x.show();
y.show();
y=++x;
x.show();
y.show();
}
Program 14.3
Write a program that overloads postfix increment operator to work
with user defined objects.
#include<iostream>
#include<conio.h>
using namespace std;
class count
{
private:
int n;
public:
count()
{
n=0;
}
26
void show()
{
cout<<"n = "<<n<<endl;
}
count operator++()
{
count temp;
n=n+1;
temp.n=n;
return temp;
}
count operator++(int)
{
count temp;
n=n+1;
temp.n=n;
return temp;
}
};
int main()
{
count x;
x.show();
++x;
x++;
x.show();
}
Arithmetic Operators:
The binary arithmetic operators such as +, - ,*, / are binary operators.
They work with two operands.
Program 14.4
Write a program that overloads binary addition operator +.
27
#include<iostream>
#include<conio.h>
using namespace std;
class add
{
private:
int a,b;
public:
add()
{
a=b=0;
}
void in()
{
cout<<"Enter a: ";
cin>>a;
cout<<"Enter b: ";
cin>>b;
}
void show()
{
cout<<"a = "<<a<<endl;
cout<<"b = "<<b<<endl;
}
add operator+(add p)
{
add temp;
temp.a=p.a+a;
temp.b=p.b+b;
return temp;
}
};
int main()
{
28
add x,y,z;
x.in();
y.in();
z=x+y;
x.show();
y.show();
z.show();
}
Program 14.5
Write a program that overloads the comparison operators = = to work
with string class. The result of comparison must be 1 if two strings are of
same length and otherwise 0.
#include<iostream>
#include<conio.h>
#include<string.h>
using namespace std;
class String
{
private:
char str[50];
public:
String()
{
str[0]='\0';
}
void in()
{
cout<<"Enter string: ";
gets(str);
}
void show()
{
29
cout<<str<<endl;
}
int operator == (String s)
{
if(string(s.str) == string(str))
return 1;
else
return 0;
}
};
int main()
{
String s1,s2;
s1.in();
s2.in();
cout<<"s1 = ";
s1.show();
cout<<"s2 = ";
s2.show();
if(s2==s1)
cout<<"Both strings are of diff length";
else
cout<<"Both strings are of equal length";
}
30
Inheritance
Inheritance:
A programming technique that is used to reuse an existing class to
build a new class is known as inheritance. The new class inherits all the
behavior of original class.
1.Reusability
Inheritance allows the developer to reuse existing code in many
situations. A class can be created once and it can be reuse again and again
to create many sub classes.
Categories of Inheritance
There are two categories of inheritance:
1. Single Inheritance
A type of inheritance in which a child class is derived from single parent
class is known as single inheritance. The child class inheritance inherits
31
all data members and member functions of the parent class. It can also add
further capabilities of its own.
Parent
Child
2. Multiple Inheritance
A type of inheritance in which a child class is derived from multiple
parent classes is known as multiple inheritance. The child class in this
inheritance inherits all data members and member functions of all parent
classes. It can also add further capabilities of its own.
Parent 1 Parent 2
Syntax
The syntax of specifying a derived class is as fellows:
Class sub _ class : specifier parent_class
{
body of the class
32
};
Program 15.1
Write a class person that has the attributes of id, name and address. It
has constructor to initialize, a member function to input and a member
function to display data members. Create another class student that
inherits person class. It has additional attributes roll number and marks. It
has member function to input and display its data members.
#include<iostream>
#include<conio.h>
#include<string.h>
#include<stdio.h>
using namespace std;
class person
{
protected:
int id;
char name[50], address[100];
public:
person()
{
id=0;
name[0]='\0';
address[0]='\0';
}
void getinfo()
{
cout<<"Enter your id: ";
cin>>id;
cout<<"Enter your name: ";
gets(name);
cout<<"Enter your address: ";
33
gets(address);
}
void showinfo()
{
cout<<"\nYour personal information is as follows:\n";
cout<<"id = "<<id<<endl;
cout<<"Name = "<<name<<endl;
cout<<"Address = "<<address<<endl;
}
};
class student : public person
{
private:
int rno, marks;
public:
student()
{
person::person();
rno=marks=0;
}
void getedu()
{
cout<<"Enter your roll no: ";
cin>>rno;
cout<<"Enter your marks: ";
cin>>marks;
}
void showedu()
{
cout<<"\nYour educational information is as follows:\n";
cout<<"Roll No = "<<rno<<endl;
cout<<"Marks = "<<marks<<endl;
}
};
int main()
34
{
student s;
s.getinfo();
s.getedu();
s.showinfo();
s.showedu();
}
Program 15.2
Write a program that declare two classes. The parent class is called
Simple that has two data members a and b to store two numbers. It also
has four members function.
#include<iostream>
#include<conio.h>
#include<string.h>
#include<stdio.h>
using namespace std;
class simple
{
protected:
int a,b;
public:
simple()
{
a=b=0;
}
void in()
{
cout<<"Enter a: ";
cin>>a;
cout<<"Enter b: ";
cin>>b;
}
35
void add()
{
cout<<"a+b = "<<a+b<<endl;
}
void sub()
{
cout<<"a-b = "<<a-b<<endl;
}
void mul()
{
cout<<"a*b = "<<a*b<<endl;
}
void div()
{
cout<<"a/b = "<<a/b<<endl;
}
};
class complex : public simple
{
public:
void add()
{
if(a<=0 || b<=0)
cout<<"Invalid values."<<endl;
else
simple::add();
}
void sub()
{
if(a<=0 || b<=0)
cout<<"Invalid values"<<endl;
else
simple::sub();
}
void mul()
36
{
if(a<=0 || b<=0)
cout<<"Invalid values"<<endl;
else
simple::mul();
}
void div()
{
if(a<=0 || b<=0)
cout<<"Invalid values"<<endl;
else
simple::div();
}
};
int main()
{
complex obj;
obj.add();
obj.in();
obj.add();
obj.sub();
obj.mul();
obj.div();
}
Program 15.3
Write a program that declare two classes and defines a relationship
between them using public inheritance.
#include<iostream>
#include<conio.h>
#include<string.h>
#include<stdio.h>
using namespace std;
37
class parent
{
public:
int a;
protected:
int b;
private:
int c;
};
class child : public parent
{
public:
void in()
{
cout<<"Enter a: ";
cin>>a;
cout<<"Enter b: ";
cin>>b;
}
void out()
{
cout<<"a = "<<a<<endl;
cout<<"b = "<<b<<endl;
}
};
int main()
{
child obj;
obj.in();
obj.out();
}
38
Program 16.1
Write a class with an integer data member , a function to input and a
function to display it. Creates an object of the class using pointer and class
its member functions.
#include<iostream>
#include<conio.h>
using namespace std;
class Test
{
private:
int n;
public:
void in()
{
cout<<"Enter number ="; cin>>n;
}
void out()
{
cout <<"The value of n ="<<n;
}
39
};
int main()
{
Test *ptr;
ptr= new Test;
ptr ->in();
ptr->in();
ptr->out();
}
Pointer to objects
A pointer can also refer to an object of a class. The member of an
object can be accessed through pointers by using the symbol ->. The
symbol is known as member access operator.
Syntax
The syntax of referencing an object with pointer is as follows:
Ptr -> member
Program 16.2
Write a class that contains an attribute name, a function to input and a
function to display name. Creates array of pointers in which each element
refers to an object of the class.
#include<iostream> #include<conio.h> using namespace std; class
Person
{
private: char name[50];
public:
void get()
{
cout<<"Enter your name:
"; cin>>name;
}
void show()
40
{
cout<<"Your name ="<<name<<endl;
}
};
int main()
{
Person *ptr[5];
int i;
for(i=0; i<5; i++)
{
ptr[i] =new Person; ptr[i]-
>get();
}
for(i=5; i<5; i++) ptr[i]-
>show();
}
Virtual Functions
Virtual means existing in effect but not in reality. A type of function
that appears to exist in some part of a program but does not exist really is
called virtual function. Virtual functions are used to implement
polymorphism. A virtual function is defined in the parent class and be
overridden in child classes. It is defined by using the keyword virtual.
Example
#include<iostream>
#include<conio.h>
using namespace std;
class A
{
public:
virtual void show()
{
cout<<"Parent class A..."<<endl;
}
};
class B : public A
{ public:
void show()
{
cout<<"Child class B..."<<endl;
}
};
class C : public A
{
public: void show()
{
cout<<"Child class C..."<<endl;
}
};
int main()
42
{
obj1;
obj2;
obj3;
A *ptr; ptr = &obj1;
ptr>show();
ptr = &obj2;
ptr->show();
ptr = &obj3;
ptr->show();
getch();
}
Early Binding
The assignment of types to variables and expressions at compilation
time is known as early binding. It is also called static binding. The early
binding occurs when everything required to call a function is known at
compile time. Early binding enables the compiler to know exactly which
function will be called when a certain statement is executed.
Late Binding
The assignment of types to variables and expressions at execution
time is known as late binding. It is also called dynamic binding. The late
binding occurs when some information to call a function is decided at run
time. The compiler does not know at compile time which function will
be executed. It provides more flexibility.
Abstract Classes
A type of class that contains any pure virtual function is called
abstract class. An abstract class cannot be used directly. It means that
no object of an abstract class can be created. However, a child class can
inherit an abstract class and use it by overriding its pure virtual fuction.
Syntax
The syntax of declaring pure virtual function is as follows:
virtual return –type function-name () = 0;
Example
#include<iostream> Parent
#include<conio.h>
using namespace std;
class Parent
{
Child1 Child2
private:
int n;
};
class Child1 : public Parent
{ }; Baby
class Child2 : public Parent
{ };
class Baby : public Child1, public Child2
{
public: void set()
{
n= 10; cout<<”n = ”<<n>>endl;
}
};
int main()
{
44
Baby obj;
obj.set();
getch();
}