0% found this document useful (0 votes)
40 views18 pages

OOP Manual

This lab manual outlines the Object Oriented Programming course for the 2nd semester of the BS (CS) program at Government Islamia Graduate College for Women, covering objectives, outcomes, recommended software, and a detailed weekly plan. The course focuses on C++ programming concepts such as inheritance, polymorphism, operator overloading, and templates through practical lab exercises. It includes a list of textbooks and references for further study.

Uploaded by

umimirza775
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)
40 views18 pages

OOP Manual

This lab manual outlines the Object Oriented Programming course for the 2nd semester of the BS (CS) program at Government Islamia Graduate College for Women, covering objectives, outcomes, recommended software, and a detailed weekly plan. The course focuses on C++ programming concepts such as inheritance, polymorphism, operator overloading, and templates through practical lab exercises. It includes a list of textbooks and references for further study.

Uploaded by

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

DEPARTMENT OF COMPUTER SCIENCE

GOVERNMENT ISLAMIA GRADUATE COLLEGE FOR WOMEN, EIDGAH ROAD FAISALABAD

LAB MANUAL

Degree Computer Science

Semester 2nd, BS (CS)

Session 2023-2027

Course Title Object Oriented Programming

Course Code CCC-302

Credit Hours 4(3-1)

Sr. # Content
1. Objectives
2. Outcomes
3. Recommended System / Software
Requirements
4. Text Books
5. References
6. Weekly Plan
7. Lab Work details

Prepared by:

Hafiza Amina Ashraf


Objectives:
• To write and execute programs in C++ to solve problems using Abstract data types, object instantiation,
inheritance, polymorphism, composition, member access control, templates, overloaded operators, exception
handling, use of C++ headers, and class libraries will be studied
• To write and execute programs in C++ to implement various inheritance and polymorphism methods.
Outcomes:
• Ability to design, implement, test and debug simple and complex programming problems using object-
oriented programming.
• To design, implement and test the implementation of a “is-a” relationships among objects using a class
hierarchy and inheritance
• To compare and contrast the notion of overloading and overriding methods in an object-oriented
language.

Recommended System / Software Requirements:


1. Intel based desktop PC of 166MHz or faster processor with at least 64 MB RAM and 100 MB free disk
space.
2. Turbo C++ compiler

Text Book:
Object Oriented programming by Tasleem Mustafa, Tariq Mahmood, Imran Saeed and Ahsan Raza Sattar
References:
1.C++ How to Program, 6/E (Harvey & Paul) Deitel & Deitel ISBN-10: 0136152503 ISBN-13: 9780136152507
Publisher: Prentice Hall
2. Java How to Program, 7/E (Harvey & Paul) Deitel & Deitel ISBN-10: 0132222205 ISBN-13:
9780132222204 Publisher: Prentice Hall

Weekly Plan
Week Program
1 Write a C++ program to implement i) Executing Member Functions
2 Write a C++ program to implement Array and Defining Member Function Outside Class
3 Write a C++ programs to implement Static Data Member
4 Write a C++ a program to implement Static Function
5 Write a C++ programs to implement Operator Overloading with Returned Value.
6 Write a C++ program to implement Overloads Postfix Increment Operator
7 Write a C++ programs to implement Overloads Binary Addition operator+.
8 Write a C++ program to implement Overloads Arithematic Assignment Operator+
9 Write a C++ program to implement Protected Access Specifier
10 Write a C++ program to implement Function Overriding
11 Write a C++ program to implement i) Public Inheritance ii) Containership
12 Write a C++ programs to implement i) Create objects using Pointer ii) Arrays of pointer to objects
13 Write a C++ program to implement i) Function Templates ii) Function Templates Accept values
14 Write a C++ programs to implement i) Template for finding Minimum Value in Array
ii) Write Class Template To store Any values in Array
15 Write a C++ program to implement i) Input the Name of Cities and Store in City.txt
ii) Reading Single Character
16 Project
Week 1:
Write a C++ program to implement i) Executing Member Functions

#include<iostream.h>
#include<conio.h>
class Marks
{
private:
int a,b,c;
public:
void in()
{
cout<<"Enter three marks:";
cin>>a>>b>>c;
}
int sum()
{
return a+b+c;
}
float avg ()
{
return (a+b+c)/3.0;
}
};
void main ()
{
Marks m;
int s;
float a;
m.in ();
s = m.sum ();
a = m.avg ();
cout<<"Sum="<<s<<endl;
cout<<"Average="<<a;
getch ();
}

Week 2:
Write a C++ program to implement Array and Defining Member Function Outside Class

#include<iostream.h>
#include<conio.h>
class Array
{
private:
int a [5];
public:
void fill ();
void display ();
int max ();
int min ();
};
void Array::fill ()
{
for (int i=0; i<5; i++)
{
cout << "Enter a [" << i << "]:";
cin>> a[i];
}
}
void Array::display ()
{
for (int i=0; i<5; i++)
cout<<"a["<<i<<"] :"<<a[i]<<endl;
}
int Array::max ()
{
int m=a [0];
for (int i=0; i<5; i++)
if(m<a[i])
m=a[i];
return m;
}
int Array::min ()
{
int m=a [0];
for (int i=0; i<5; i++)
if(m>a[i])
m=a[i];
return m;
}
void main ()
{
Array arr;
arr. fill ();
cout<<"You entered the following values:"<<endl;
arr. display ();
cout<<"Maximum value = "<<arr.max () <<endl;
cout<<"Minimum value = "<<arr.min ();
getch ();
}

Week 3:
Write C++ programs to implement Static Data Member.

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class Student
{
private:
static int r;
int rno,marks;
char name [50];
public:
Student ()
{
r++;
rno = r;
}
void in ()
{
cout<<"Enter name:";
gets(name);
cout<<"Enter marks:";
cin>>marks;
}
void show ()
{
cout<<"Roll no:"<<rno<< endl;
cout<<"Name:" <<name<<endl;
cout<<"Marks:"<<marks<<endl;
}
};
int Student::r = 0;
void main ()
{
Student s1,s2,s3;
s1.in();
s2.in();
s3.in();
s1. show ();
s2. show ();
s3. show ();
getch ();

Week 4:
Write C++ a program to implement Static Function

#include<iostream.h>
#include<conio.h>
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;
void main ()
{
clrscr ();
Yahoo::show ();
yahoo x, y;
x.show ();
Yahoo z;
x.show ();
getch ();
}

Week 5:
Write C++ programs to implement Operator Overloading with Returned Value.

#include<iostream.h>
#include<conio.h>
class Count
{
private:
int n;
public:
Count ()
{
n = 0;
}
void show ()
{
cout<<"n="<<n<<endl;
}
Count operator ++ ()
{
Count temp;
n=n+1;
temp.n=n;
return temp;
}
};
void main ()
{
clrscr ();
Count x, y;
x.show ();
y. show ();
y=++x;
x.show ();
y. show ();
getch ();
}

Week 6:
Write C++ program to implement Overloads Postfix Increment Operator

#include<iostream.h>
#include<conio.h>
class Count
{
private:
int n;
public:
Count ()
{
n=0;
}
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;
}
};
void main ()
{
clrscr ();
Count x;
x.show ();
++x;
x++;
x.show ();
getch ();
}

Weak 7:
Write C++ programs to implement Overloads Binary Addition operator+.

#include<iostream.h>
#include<conio.h>
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;
}
};
void main ()
{
clrscr ();
Add x, y, z;
x.in ();
y.in ();
z=x+y;
x.show ();
y. show ();
z. show ();
getch ();
}

Weak 8:
Write C++ program to implement Overloads Arithematic Assignment Operator

#include<iostream.h>
#include<conio.h>
class Read
{
private:
int days, pages;
public:
Read ()
{
days=pages=0;
}
void in ()
{
cout<<"How many days have you read?";
cin>>days;
cout<<"How many pages have you read?";
cin>>pages;
}
void show ()
{
cout<<"You have read"<<pages<<"pages in"<<days<<"days"<<endl;
}
void operator += (Read r)
{
days=days+r. days;
pages=pages+r.pages;
}
};
void main ()
{
clrscr ();
Read r1.r2;
r1.in ();
r2.in ();
cout<<"Reading number 1..."<<endl;
r1. show ();
cout<<"Reading number 2..."<<endl;
r2. show ();
cout<<"Adding r1 to r2 using +=operator..."<<endl;
r2+=r1;
cout<<"Total reading is as follow:"<<endl;
r2. show ();
getch ();
}

Weak 9:
Write C++ program to implement Protected Access Specifier

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
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:";
gets(address);
}
void Show Info ()
{
cout<<"Your personal information is as follow:";
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 rol no:";
cin>>rno;
cout<<"Enter your marks:";
cin>>marks;
}
void ShowEdu ()
{
cout<<"Your educational information is as follow:";
cout<<"Roll No="<<rno<<endl;
cout<<"Marks="<<marks<<endl;
}
};
void main ()
{
clrscr ();
Student s;
s. GetInfo ();
s. GetEdu ();
s. ShowInfo ();
s. ShowEdu ();
getch ();
}

Weak 10:
Write C++ program to implement Function Overriding

#include<iostream.h>
#include<conio.h>
class Simple
{
protected:
int a, b;
public:
Simple ()
{
a=b=0;
}
void in ()
{
cout<<"Enter a:";
cin>>a;
cout<<"Enter b:";
cin>>b;
}
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 ()
{
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 ();
}
};
void main ()
{
clrscr ();
Complex obj;
obj.add ();
obj.in ();
obj.add ();
obj.sub ();
obj.mul ();
obj.div ();
getch ();
}

Weak 11:
Write C++ program to implement i) Public Inheritance ii) Containership

i) Public Inheritance

#include<iostream.h>
#include<conio.h>
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;
}
};
void main ()
{
clrscr ();
Child obj;
obj.in ();
obj.out ();
getch ();
}

ii) Containership

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class Result
{
private:
int marks [3];
public:
void in ()
{
for (int i=0; i<3; i++)
{
cout<<"Enter marks:";
cin>>marks[i];
}
}
void show ()
{
int t=0;
cout<<"\nResult Card:\n";
for (int i=0; i<3; i++)
{
cout<<"Marks="<<marks[i]<<endl;
t=t+marks[i];
}
cout<<"Total Marks="<<t<<endl;
cout<<"Average Marks="<<float(t)/3.0;
}
};
class Student
{
private:
int rno;
char name [50];
Result res;
public:
void in ()
{
cout<<"Enter your Roll No:";
cin>>rno;
cout<<"Enter your name:";
gets(name);
res.in ();
}
void show ()
{
cout<<"\nPersonal Information:\n";
cout<<"Roll No="<<rno<<endl;
cout<<"Name="<<name<<endl;
res. show ();
}
};
void main ()
{
clrscr ();
Student obj;
obj.in ();
obj. show ();
getch ();
}
Weak 12:
Write C++ programs to implement i) Create objects using Pointer ii) Arrays of pointer to objects

i) Create objects using Pointer


#include<iostream.h>
#include<conio.h>
class Test
{
private:
int n;
public:
void in ()
{
cout<<"Enter number:";
cin>>n;
}
void out ()
{
cout<<"The value of n="<<n;
}
};
void main ()
{
clrscr ();
Test*ptr;
ptr=new Test;
ptr->in ();
ptr->out ();
getch ();
}

ii) Arrays of pointer to objects

#include<iostream.h>
#include<conio.h>
class Person
{
private:
char name [50];
public:
void get ()
{
cout<<"Enter your name:";
cin>>name;
}
void show ()
{
cout<<"Your name="<<name<<endl;
}
};
void main ()
{
clrscr ();
Person*ptr [5];
int i;
for (i=0; i<5; i++)
{
ptr[i]=new Person;
ptr[i]->get ();
}
for (i=0; i<5; i++)
ptr[i]->show ();
getch ();
}

Weak 13:
Write C++ program to implement i) Function Templates ii) Function Templates Accept values.

i)Function Templates

#include<iostream.h>
#include<conio.h>
template<class Type>
Type Max (Type a, Type b)
{
if(a>b)
return a;
else
return b;
}
void main ()
{
clrscr ();
int n;
float m;
n=Max (10,80);
cout<<"Maximum of integers:"<<n<<endl;
m=Max (5.6,8.90);
cout<<"Maximum of two floats:"<<m<<endl;
getch ();
}

ii) Function Templates Accept values.

#include<iostream.h>
#include<conio.h>
template <class Type>
void show (Type a)
{
cout<<a<<endl;
}
void main ()
{
clrscr ();
show("Hello");
show (100);
show (50.75);
show ('*');
getch ();
}
Week 14:
Write C++ programs to implement i) Template for finding Minimum Value in Array
ii) Write Class Template To store Any values in Array

i) Template for finding Minimum Value in Array

#include<iostream.h>
#include<conio.h>
template <class T>
T findMin (T arr [], int n)
{
int i;
T min;
min=arr [0];
for (i=0; i<n; i++)
{
if (min > arr[i])
min=arr[i];
}
return(min);
}
void main ()
{
int iarr[]={5,4,3,2,1};
char carr[]={'z','y','c','b','a'};
double darr[]={3.3,5.5,2.2,1.1,4.4};
cout<<"Genratic Function to find Minimum from Array"<<endl;
cout<<"Integer Minimum is:"<<findMin(iarr,5) <<"\n";
cout<<"Character Minimum is:"<<findMin(carr,5) <<"\n";
cout<<"Double Minimum is:"<<findMin(darr,5) <<"\n";
getch ();
}

ii) Write Class Template To store Any values in Array

#include<iostream.h>
#include<conio.h>
template<class Type>
class Test
{
private:
Type arr[3];
public:
void input ()
{
for (int i=0; i<3; i++)
cin>>arr[i];
}
void output ()
{
cout<<"Array values are as follow:\n";
for (int i=0; i<3; i++)
cout<<arr[i]<<"\t";
cout<<endl;
}
};
void main ()
{
clrscr ();
Test <int> x;
Test <char>y;
cout<<"Enter three integers:"<<endl;
x.input();
cout<<"Enter three characters:"<<endl;
y. input ();
x.output ();
y. output ();
getch ();
}

Week 15:
Write a C++ program to implement i) Input the Name of Cities and Store in City.txt
ii)Reading Single Character

i)Input the Name of Cities and Store in City.txt

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
void main ()
{
clrscr ();
char city [50];
ofstream file("c:\\city.txt.");
for (int i=0; i<5; i++)
{
cout<<"Enter the name of any city:";
cin>>city;
file<<city<<'\n';
}
file. close ();
getch ();
}

ii)Reading Single Character

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<fstream.h>
#include<ctype.h>
void main ()
{
clrscr ();
char ch;
int t, v;
t=v=0;
ifstream in("c:\\char.txt");
while (! in.eof ())
{
in.get(ch);
ch=tolower(ch);
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')
v++;
t++;
cout<<ch<<endl;
}
cout<<"Total characters:"<<t<<endl;
cout<<"Total vowels:"<<v<<endl;
in. close ();
getch ();
}

Weak 16:
Project

SEMESTER PROJECT

Submission: Last Week of Semester

ATM Machine
Develop an ATM project using object-oriented programming (OOP) that allows users to check their
balance, deposit, withdraw and transfer money, with appropriate validation and secure transaction
handling.

You might also like