Professional Documents
Culture Documents
Class: BEE-13D
Fall 2022
Lab06: Constructors
Instructor: Mehreen
Tahir
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.
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
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();
};
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;
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() {
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
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>
class Student {
private:
string Name;
int Age;
int* p;
public:
Student(); // Just Adding Prototypes Here
Student(string t_name, int t_age);
void print_data();
};
Code
#include<iostream>
#include<string.h>
#include "Header.h"
using namespace std;
Student::Student()
{
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;
Output:
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#.