Lab 07 Solutions
Lab 07 Solutions
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;
};
void display() {
cout << "Doctor's Name: " << name << ", Age: " << age << ",
Salary: " << salary << endl;
}
};
void display() {
cout << "Engineer's Name: " << name << ", Age: " << age << ",
Salary: " << salary << endl;
}
};
int main() {
Doctor doc;
Engineer eng;
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;
}
};
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");
}
};
void PrintDetail() {
printf("Student details are being printed.\n");
}
void Write() {
printf("Student is writing.\n");
}
};
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;
}