You are on page 1of 10

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.
Code:
Header.h:
#include<iostream>
#include<string.h> // for string
using namespace std;
class Sandwich { // defined prototype and variables
private: // can be accessed inside the class
string filling;
double price;
bool is_ready;
string size;

public:
Sandwich();
Sandwich(string,string , double );

void setsize(string);
void setFilling(string);
void setPrice(double);
bool check_status();
void printData();
};
Code 1:
#include<iostream>
#include"Header.h" // unstandardfile
using namespace std;

CS 212: Object Oriented Page 3


Programming
/////////////////////
Sandwich::Sandwich(){
filling = "meat, poultry, fish ";
size = "large";
price = 12;
Sandwich::setFilling(filling); // will get is_ready=1
}
/////////////////////
Sandwich::Sandwich(string a,string b, double c) {
filling = a;
size = b;
price = c;
Sandwich::setFilling(a);
}
/////////////////////
void Sandwich::setFilling(string a) {
is_ready = 1;
filling = a;
}
/////////////////
void Sandwich::setsize(string a) {

filling = a;
}
/////////////////
void Sandwich::setPrice(double a) {

price = a;
}
//////////////////
bool Sandwich::check_status() {
is_ready = 1;
if (is_ready)
return is_ready;
else
return 0;

}
///////////////////
void Sandwich::printData() {
if (is_ready)
{
cout << "The sandwich is ready and the details are below" << endl;
cout << " the filling is " << filling << endl;
cout << " the size is " << size << endl;
cout << " the price is " << price << endl;
}
else
cout << "not ready";
}
Code 2:
#include<iostream>
#include"Header.h"
using namespace std;
int main() {

CS 212: Object Oriented Page 4


Programming
Sandwich s1("eggs, cheese, vegetables.", "regular", 8);
Sandwich s2; // definging the object

Sandwich s3; // definig the object


s2.setFilling("ketchup"); s2.setsize("small"); s2.setPrice(14);
Sandwich s4 = s2; // copy constructor
s1.printData();
cout << endl << endl;
s2.printData(); //
cout << endl << endl;
s3.printData(); // print the data
cout << endl << endl;
cout << "this is a copy constructor"<<endl;
s4.printData(); // print the data
}
Output:

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

CS 212: Object Oriented Page 5


Programming
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)
 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
Code:
Header
#include<iostream>

#include<string> // this is for string

using namespace std;

class Student {
private:
string Name;
int Age;
int* p;

public:
Student(); // Just Adding Prototypes Here
Student(string t_name, int t_age);

void set_name(string name);


void set_age(int age);

void print_data();

};
Code
#include<iostream>

#include<string.h>
#include "Header.h"
using namespace std;

Student::Student()
{

CS 212: Object Oriented Page 6


Programming
int arr[3] = { 1, 2, 3 };
string Name = "";
Age = 0;
p = arr;
}
Student::Student(string t_name, int t_age)
{
int arr[3] = { 1, 2, 3 };
Name = t_name;
Age = t_age;
p = arr;
}
void Student::set_name(string name)
Name = name;
}
void Student::set_age(int age)
{
Age = age;
}

void Student::print_data() {
cout << ">> Name of Student = " << Name << endl;
cout << ">> Age of Student = " << Age << endl;
cout << ">> Address of Student = " << &p << endl;
}

Code 2
#include<iostream>
#include<string.h>
#include "Header.h"
using namespace std;

int main()
{
string in_name;
int in_age;
cout << "Enter Name of the Student: ";
cin >> in_name;
cout << "Enter Age of the Student: ";
cin >> in_age;
cout << endl;

// Default Constructor
Student std1;
std1.set_name(in_name);
std1.set_age(in_age);
std1.print_data();
cout << endl;

cout << endl << endl;

CS 212: Object Oriented Page 7


Programming
Student std2(in_name, in_age);
std2.print_data();
cout << endl;

// Implicit Copy constructor is called here


cout << endl << endl;
Student std3 = std1;
std3.print_data();

// Checking Copy Contructor after modifications in Base Object


std1.set_name("Umr");
std1.set_age(456);
cout << endl << endl;
std1.print_data();
cout << endl <<endl;
std3.print_data();
system("pause");
return 0;
}

Output:

CS 212: Object Oriented Page 8


Programming
CS 212: Object Oriented Page 9
Programming
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


Programming 10

You might also like