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

Lab 07 Solutions

Uploaded by

Amna Faisal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Lab 07 Solutions

Uploaded by

Amna Faisal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Task 1

Static Array

#include <iostream>
using namespace std;

class StaticArrayExample {
private:
static const int size = 5;
int arr[size];

public:
StaticArrayExample() {
// Initialize the array with numbers from 1 to 5
for (int i = 0; i < size; ++i) {
arr[i] = i + 1;
}
}

void display() {
// Print the squares of the array elements
for (int i = 0; i < size; ++i) {
cout << arr[i] * arr[i] << " ";
}
cout << endl;
}
};

int main() {
StaticArrayExample example;
example.display(); // Output: 1 4 9 16 25
return 0;
}

Dynamic Array

#include <iostream>
using namespace std;

class DynamicArrayExample {
private:
int* arr;
int size;

public:
DynamicArrayExample(int s) {
size=s;
arr = new int[size]; // Allocate dynamic array
// Initialize the array with numbers from 1 to size
for (int i = 0; i < size; ++i) {
arr[i] = i + 1;
}
}

~DynamicArrayExample() {
delete[] arr; // Deallocate dynamic array
}

void display() {
// Print the squares of the array elements
for (int i = 0; i < size; ++i) {
cout << arr[i] * arr[i] << " ";
}
cout << endl;
}
};

int main() {
int n;
cout << "Enter the size of the array: ";
cin >> n;
DynamicArrayExample example(n);
example.display(); // Output: Squares of numbers from 1 to n
return 0;
}

Task 2

#include <iostream>
using namespace std;

class Person {
protected:
string name;
int age;
double salary;
};

class Doctor : public Person {


public:
void setDetails(string n, int a, double s) {
name = n;
age = a;
salary = s;
}

void display() {
cout << "Doctor's Name: " << name << ", Age: " << age << ",
Salary: " << salary << endl;
}
};

class Engineer : public Person {


public:
void setDetails(string n, int a, double s) {
name = n;
age = a;
salary = s;
}

void display() {
cout << "Engineer's Name: " << name << ", Age: " << age << ",
Salary: " << salary << endl;
}
};

int main() {
Doctor doc;
Engineer eng;

doc.setDetails("Dr. Smith", 45, 120000);


eng.setDetails("Engr. Johnson", 38, 95000);

doc.display();
eng.display();

return 0;
}

Task 3
#include <iostream>
using namespace std;

class Date {
public:
int day, month, year;

void display() {
cout << "Date: " << day << "/" << month << "/" << year << endl;
}
};

class Time {
public:
int hour, minutes, seconds;

void display() {
cout << "Time: " << hour << ":" << minutes << ":" << seconds <<
endl;
}
};

class Time_Date : public Date, public Time {


public:
void display() {
cout << "Date: " << day << "/" << month << "/" << year << " ";
cout << "Time: " << hour << ":" << minutes << ":" << seconds <<
endl;
}
};

int main() {
Time_Date td;
td.day = 22;
td.month = 8;
td.year = 2024;
td.hour = 14;
td.minutes = 30;
td.seconds = 45;

td.display();

return 0;
}
Task 4

#include <iostream>
using namespace std;

class Person {
public:
double weight, height;
string gender;

void walk() {
printf("Person is walking.\n");
}

void sit() {
printf("Person is sitting.\n");
}
};

class Student : public Person {


public:
int ID;
string firstName, lastName;
string graduation;

void PrintDetail() {
printf("Student details are being printed.\n");
}

void Write() {
printf("Student is writing.\n");
}
};

class GraduationStudent : public Student {


public:
string universityName;
int yearGraduation;

void Display() {
printf("Displaying Graduation Student's information:\n");
printf("University: %s, Graduation Year: %d\n",
universityName.c_str(), yearGraduation);
printf("Student ID: %d, Name: %s %s, Graduation: %s\n", ID,
firstName.c_str(), lastName.c_str(), graduation.c_str());
printf("Weight: %.2f, Height: %.2f, Gender: %s\n", weight,
height, gender.c_str());
}
};

int main() {
GraduationStudent gs;

gs.ID = 101;
gs.firstName = "John";
gs.lastName = "Doe";
gs.graduation = "2022";
gs.universityName = "XYZ University";
gs.yearGraduation = 2022;
gs.weight = 70.5;
gs.height = 175.2;
gs.gender = "Male";

gs.walk();
gs.sit();
gs.PrintDetail();
gs.Write();
gs.Display();

return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy