Lab 06
Lab 06
Fall 2023
Lab 06
Instructions:
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.
• Create two Rectangle objects r1 and r2 with different values for length and width, and copy r1
to r2.
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.
• Create two Person objects p1 and p2 with different values for name and age, and copy p1 to
p2.
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*);
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.
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;
}
Let’s create a class Time to work with a 12-hour formatted time having the following attributes:
int* hours
int* minutes
int* seconds
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