0% found this document useful (0 votes)
9 views

lab5

This document outlines Lab 5 for an Object Oriented Programming course at UCP Lahore, focusing on concepts such as overloaded constructors, deep vs shallow copy, and the use of pointers within classes. It includes lab objectives, instructions for coding practices, sample tasks with solutions, and additional lab tasks related to creating classes like 'Student', 'BankAccount', and 'NextBridge'. The document emphasizes the importance of understanding OOP principles and proper coding techniques.

Uploaded by

hirrah21
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)
9 views

lab5

This document outlines Lab 5 for an Object Oriented Programming course at UCP Lahore, focusing on concepts such as overloaded constructors, deep vs shallow copy, and the use of pointers within classes. It includes lab objectives, instructions for coding practices, sample tasks with solutions, and additional lab tasks related to creating classes like 'Student', 'BankAccount', and 'NextBridge'. The document emphasizes the importance of understanding OOP principles and proper coding techniques.

Uploaded by

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

Object Oriented Programming

Section 06

Session: Fall 2022


Faculty of Information Technology

UCP Lahore, Pakistan


Contents
Session: Fall 2022 .................................................................................................................................... 1
Lab 5: concepts of OOP( overloaded constructor, deep copy vs shallow, copy using pointers inside
the class and this pointer)................................................................................................................... 3
Lab Objectives: .................................................................................................................................... 3
Instructions: ........................................................................................................................................ 3
Sample task: ............................................................................................................................................ 4
Sample task Solution:.............................................................................................................................. 4
Sample task output ................................................................................................................................. 7
LAB TASKS: .............................................................................................................................................. 8
Task 1 ................................................................................................................................................ 8
Task 2 ................................................................................................................................................ 9
Lab 5: concepts of OOP( overloaded constructor, deep copy vs shallow, copy
using pointers inside the class and this pointer)

Lab Objectives:
The basic purpose of this laboratory is revision of some preliminary concepts of ++ that has been
covered in the course of Introduction to Computing and Programming Fundamentals. Its objective is
to:

• Recall students previously learned basic concepts.


• Understanding of object-oriented programming.
• Understanding problem statements and designing an appropriate solution.

Instructions:

• Indent your code


• Comment your code
• Use meaningful variable names
• Plan your code carefully on a piece of paper before you implement it.
• Name of the program should be same as the task name. i.e. the first program should be
Task_1_01.cpp
Sample task:
Create a student class having attributes id,name,oop marks, calculus marks, ds marks now perform deep
copy in parameterized constructor and setter where required.find total marks by adding all 3 subject
marks
Create getter setter of each attributes create default and parameterize constructors

Sample task Solution:


#include<iostream>
using namespace std;
class Student{
private:
int id;
char* name;
float OOP_marks;
float Cal_marks;
float Ds_marks;

public:
Student();
Student(int id, char*name, float om, float cm, float dm);
Student(const Student &obj);
void setname(char* n);
char* getname();
void setid(int id);
int getid();
void display();
void set_oopmarks(float om);
void set_calmarks(float cm);
void set_dsmarks(float dm);
float get_oopmarks();
float get_calmarks();
float get_dsmarks();
float findtotal();

};
Student::Student(){
id = 0;
name = nullptr;
OOP_marks = 0.0;
Cal_marks = 0.0;
Ds_marks = 0.0;
cout << "its a default constructor" << endl;

}
Student::Student(int id, char*name, float om, float cm, float dm){
this->id = id;
int size = 0;
for (int i = 0; name[i] != '\0'; i++){
size++;
}
this->name = new char[size + 1];
for (int i = 0; i < size + 1; i++)
{
this->name[i] = name[i];
}
this->name[size] = '\0';
OOP_marks = om;
Cal_marks = cm;
Ds_marks = dm;
cout << "its a parameterized constructor" << endl;
}
Student::Student(const Student &obj){
cout << "its a copy constructor" << endl;

id = obj.id;
int size = 0;
for (int i = 0; obj.name[i] != '\0'; i++){
size++;
}
name = new char[size + 1];
for (int i = 0; i < size + 1; i++)
{
name[i] = obj.name[i];
}
name[size] = '\0';

OOP_marks = obj.OOP_marks;
Cal_marks = obj.Cal_marks;
Ds_marks = obj.Ds_marks;

void Student::setname(char*name){
int size = 0;
for (int i = 0; name[i] != '\0'; i++){
size++;
}
this->name = new char[size + 1];
for (int i = 0; i < size + 1; i++)
{
this->name[i] = name[i];
}
this->name[size] = '\0';
}
char* Student::getname(){
return name;
}
void Student::setid(int id){
this->id = id;
}
int Student::getid(){
return id;
}
void Student::set_calmarks(float cm){
Cal_marks = cm;
}
void Student::set_oopmarks(float om){
OOP_marks = om;
}
void Student::set_dsmarks(float dm){
Ds_marks = dm;
}
float Student::get_calmarks(){
return Cal_marks;
}

float Student::get_oopmarks(){
return OOP_marks;
}
float Student::get_dsmarks(){
return Ds_marks;
}

float Student::findtotal(){
float total;
total = Ds_marks + OOP_marks + Cal_marks;
return total;

void Student::display(){

cout << "student id is " << id << endl;


cout << "student name is " << name << endl;
cout << "student oop marks are " << OOP_marks << endl;
cout << "student cal marks are " << Cal_marks << endl;
cout << "student ds marks are " << Ds_marks << endl;
cout << "student total marks are " << findtotal() << endl;
}

int main(){

Student s(123, "abc", 89, 70, 68);


s.display();
Student s1;
Student s9=s1;

s1.setid(9087);
s1.setname("hira");
s1.set_calmarks(90.7);
s1.set_dsmarks(96.12);
s1.set_oopmarks(99.2);
cout << "student " << s9.getname() << " total marks are " << s9.findtotal() <<
" out of 300" << endl;
system("pause");

return 0;

}
Sample task output
LAB TASKS:

Task 1
Write a code for a class ‘BankAccount’. A basic idea of the class is shown below:
copy constructor, deep copy shallow copy
- means private

+ means public
BankAccount

- Name: char *
- Address: char *
- PhoneNumber: char *
- Account_Type: char *
- Account_Balance: float
- Account_Number: int
+ BankAccount()
+ BankAccount(char *, char *, char *, char *, float, int)
+ BankAccount(BankAccount&obj)
+ void setname(char* n)(for all private member except const
attribute)
+ char* getName() (for all attributes)

When an object of this class is instantiated, it is always done by default constructor, parameterized
constructor using having 5 parameters or by creating a copy of an already existing object.
A bank account number can have 4 or more digits. This number will be unique for every account.
Use this pointer in overloading constructor and setters and also do deep copy in setter and overloaded
constructor
Task 2
Next Bridge software house Lahore requires an application that calculates employee’s increment
based on the given criteria. For the purpose software house wants his software engineer to design the
application using object-oriented approach. The class “NextBridge” is required to create with
attributes and functions as mentioned below.

- name: char*
- address: char*
- phoneNumber: char*
- level: char* //fresh,Junior,sensior
- experience:int //in years
- currentSalary: double
- increment: double
Methods:
// Constructors( parameterize constructor, copy
constructor )

// Destructor

// Calculate increment

// Display Function (parameterized function)

Increment criteria
Experience Increment
0 0%
01- 02 10 %
02- 06 20%
06 to onward 40%

When an object of the class is instantiated, it is always done by either using an overloaded constructor
having 7 parameters or by creating a copy of an already existing object. Examples of such instantiations
are given below:

NextBridge obj (“Name”, “Address”, “Ph:0300-123”, “fresh”,2,10000,0);


NextBridge obj2 = obj1; //copies the contents of obj1 in obj2

Use this pointer in overloading constructor and setters and also do deep copy in setter and overloaded
constructor

You might also like