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

Lab 06

The document outlines the instructions for a lab assignment on Object Oriented Programming for Fall 2023, detailing tasks that involve creating various C++ classes such as Rectangle, Person, Circle, and Complex Number. Each task requires specific implementations including constructors, destructors, copy constructors, and operator overloading, while adhering to coding conventions and avoiding built-in functions. Students are also instructed to manage dynamic memory and implement features like sorting and static variables within their classes.

Uploaded by

asifhuzaifa123
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Lab 06

The document outlines the instructions for a lab assignment on Object Oriented Programming for Fall 2023, detailing tasks that involve creating various C++ classes such as Rectangle, Person, Circle, and Complex Number. Each task requires specific implementations including constructors, destructors, copy constructors, and operator overloading, while adhering to coding conventions and avoiding built-in functions. Students are also instructed to manage dynamic memory and implement features like sorting and static variables within their classes.

Uploaded by

asifhuzaifa123
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Object Oriented Programming

Fall 2023

Lab 06

Instructions:

1. Comment your code.


2. Use meaningful variable names.
3. Plan your code carefully on a piece of paper before you implement it.
4. Name of the program should be same as the task name. i.e. the first program should be
Task_1.cpp
5. Complete each task in 3 files.
6. You are not allowed to use any built-in functions
7. You are required to follow the naming conventions as follow:
a. Variables: firstName; (no underscores allowed)
b. Function: getName(); (no underscores allowed)
c. ClassName: BankAccount (no underscores allowed)

Students are required to complete the following tasks in lab timings.


Task 1: (Shallow Copy of the Object)

Write a program in C++ to create a class Rectangle with attributes length and width.

• Implement a constructor that takes two parameters and initializes the attributes.

• Implement a copy constructor that performs a shallow copy of the object.

• Create two Rectangle objects r1 and r2 with different values for length and width, and copy r1
to r2.

• Modify the values of length and width of r1.

• Display the modified values of length and width of r2?

Task 2: (Deep Copy of the Object)

Write a program in C++ to create a class called “Person” with attributes name, age and constant
cnic. Set using constructor initializer list.

• Implement a constructor that takes two parameters and initializes the attributes.

• Implement a copy constructor that performs a deep copy of the object.

• Create two Person objects p1 and p2 with different values for name and age, and copy p1 to
p2.

• Modify the value of name of p1.

• Display the modified values of name of p2

Task 3: (Use of constant functions, parameters, and attributes)

• Define a Circle class that represents a circle with a radius.


• The pi attribute is a constant attribute, which is initialized with a constant value of pi.
• The getArea() function is a constant function that calculates the area of the circle and returns
it, and it is marked with the const keyword at the end of the function declaration, indicating
that it does not modify the object.
• The printInfo() function takes a constant parameter message of type std::string, which is
passed by reference to avoid unnecessary copying, and it is also marked as const, indicating
that it does not modify the object.
• The setRadius() function is a non-constant function that modifies the radius of the circle.
• Create a const Circle object, like c2, we are not allowed to call non-constant functions on it,
as it is considered a constant object. Attempting to modify a constant object using a non-
constant function will result in a compilation error.

Task 4: (Dynamic Array of Persons)

Create a C++ program that defines a class called Person with attributes
• Name(char*)
• Age(int)
• Address(char*)
• date of birth (char*)
1. Implement appropriate setters and getters function for all the variables.
2. Implement the default constructor of the class to set the values of data members.
3. Implement the destructor to deallocate the memory if any.
4. Implements a getInput function to take the input from user in appropriate data members.
5. Implement the display function to display the person’s details.
6. Implement a function to sort the persons based on their ages in ascending order.
7. In main() function implement a dynamic array to store a group of persons. Allow the user to
input details for each person and dynamically adjust the size of the array to accommodate all
persons.
Person.h
#pragma once
#ifndef PERSON_H
#define PERSON_H
class Person
{
private:
char* name;
int age;
char* address;
char* dateOfBirth;
public:
// Default constructor
Person();
// Parameterized constructor
Person(const char*, int a, const char*, const char*);
// Destructor
~Person();
// Getter and Setter functions
const char* getName() const;
void setName(const char*);
int getAge() const;
void setAge(int);
const char* getAddress() const;
void setAddress(const char*);
const char* getDateOfBirth() const;
void setDateOfBirth(const char*);

// Function to get input for a person


void getInput();

// Function to display details of a person


void display() const;

//sorting persons based on age


static void Sort(Person*, int);
};
#endif

Task 5: (Static Variable and Function)

8. Extend the Person class with a static variable to keep track of the number of objects created.
9. Update the constructor and implement parameterized constructor as per the need.
10. Implement a static function that returns the total number of persons created.
11. Create instances of the Person class, and display the total count after each object creation.
12. Enhance the Person class to include a private static variable that represents the average age
of all created persons
13. Implement a static function to calculate and update this average whenever a new person is
added. Create instances of the Person class, and display the average age after each object
creation.

Task 6: (Math Operators)

Write a C++ program to create a complex number class. Write getter setter, constructor,
destructor, and all overloaded operators needed to run the given driver code.
class complex
{
private:
int real;
int imag;
public:
//All functions that are needed.
};
int main()
{
complex c1(5,7); // for complex numbers, such as 5+7i
complex c2(5); // for numbers without imaginary part as 5 + 0i
complex c3();
complex c4();
c3 = c1+c2;
c4 = c1-c2;
c5 = c3*c4;
c6 = c4/c5;
return 0;
}

Task 7: (Comparison Operators)

Let’s create a class Time to work with a 12-hour formatted time having the following attributes:

int* hours
int* minutes
int* seconds

Provide the following functions:


1 Copy Constructor
2 Destructor
3 Overloaded constructor with default values
4 Getter of each attribute (setters are NOT required)
5 operator ==
6 operator >
7 operator !=
8 operator <
9 operator <=
10 operator >=

Make sure to check that the time remains valid in all the functions that are:
A. 1 <= hours <=12
B. 0 <= minutes < 60
C. 0 <= seconds < 60

You might also like