You are on page 1of 4

Department of Computing

CS 212: Object Oriented Programming

Class: BEE-13D
Fall 2022

Lab06: Constructors

Date: 17th October, 2022

Time: 9:00 a.m. - 12:00 p.m.

Instructor: Mehreen
Tahir

Lab Engineer: Mehwish Kiran


Introduction
In this lab Students will implement tasks using Class objects and will come to know how they can
differentiate in default, parameterized, copy and overloaded constructors.

Objective
After performing this lab, the student should be able to learn how to overload a constructor, use
copy constructors and what are the scenarios where overloaded constructors can be used.

User Defined Copy Constructor


The copy constructor is a constructor which creates an object by initializing it with an object of
the same class, which has been created previously. The copy constructor is used to:

 Initialize one object from another of the same type.


 Copy an object to pass it as an argument to a function.

Copy an object to return it from a function.

Example

#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 getX()            {  return x; }
    int getY()            {  return y; }
};
 
int main()
{
    Point p1(10, 15); // Normal constructor is called here
    Point p2 = p1; // Copy constructor is called here
 

CS 212: Object Oriented Page 2


Programming
Task#1
Implement a Sandwich class that includes data members to represent a Sandwich filling, size,
is_ready, and price. The class interface includes methods that provide appropriate access to the data
members (e.g., methods to get/set the Sandwich's data).

class Sandwich{
string filling;
double price;
bool is_ready;
……
public:
Sandwich();
Sandwich(string filling, double price);

void setFilling(string);
void setPrice(double);
bool check_status();
void printData();
};

Provide implementations of default constructor and parameterized constructor. The setFilling()


function sets the filling, and also sets the is_ready variable to true.

Task # 2
Write a Student class in C++
A student object should hold information about a student in your grade book and should have the
following details:
 string name
 int age
 int *p

You are required to do the following


 Write a default constructor that initializes name, age and p pointer that points to array of size
3. Add marks in the p array for 3 quizzes.
 Create object s1.
 Create object s2 exactly similar to s1. (Use default copy constructor supplied by the
compiler)
 Print the values of s1 and s2 along with address of pointer p
 Now change name and marks for s1 using setter function

 Print the values of s1 and s2 along with address of pointer p (calling the same print function
again)

CS 212: Object Oriented Page 3


Programming
 Change your code and write a copy constructor for this class
 Re-run your code
Note the difference in the outputs between compiler supplied copy constructor and your constructor

IMPORTANT

 Apply Object Oriented Programming concepts to ensure that data is private and accessed
properly through public interface comprising of set and get functions.
 Split implementation across multiple files (Header Files and Source Code Files) to ensure
that application design is modular.
 You are encouraged to use good programming conventions by entering appropriate
comments, using indentations, and using descriptive variable names in your programs.

Deliverables: Complete lab manual by performing all tasks. Copy paste your code and screen
shot of console window as a solution of each task. You are required to upload the lab tasks on
LMS and the name of the task must be in this format YourFullName_reg#.

CS 212: Object Oriented Page 4


Programming

You might also like