0% found this document useful (0 votes)
16 views9 pages

Oop lab 5 -l1f23bsds0039

Uploaded by

saad
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)
16 views9 pages

Oop lab 5 -l1f23bsds0039

Uploaded by

saad
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/ 9

Name: Maryam Farukh

Registration no: L1F23BSDS0039


Task 1:
#include <iostream>
#include <string>
using namespace std;

class Person {
private:
string name;
int age;
string address;

public:
// Default constructor
Person() : name("Ali"), age(32), address("Dubai, UAE") {}

// Constructor with one parameter


Person(string personName) : name(personName), age(32), address("Dubai, UAE") {}

// Constructor with two parameters


Person(string personName, int personAge) : name(personName), age(personAge), address("Dubai,
UAE") {}

// Constructor with three parameters


Person(string personName, int personAge, string personAddress)
: name(personName), age(personAge), address(personAddress) {}

// Method to display information


void displayInfo() const {
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
cout << "Address: " << address << endl;
}
}
Task 2:
#include <iostream>
using namespace std;

class MyNum {
private:
int number;

public:
// Parameterized constructor with default argument
MyNum(int num = 0) : number(num) {}

// Setter for number


void setNumber(int num) {
number = num;
}

// Getter for number


int getNumber() const {
return number;
}

// Convert to positive number


void toPositiveNumber() {
if (number < 0) {
number = -number;
}
}

// Convert to negative number


void toNegativeNumber() {
if (number > 0) {
number = -number;
}
}

// Display the number


void display() const {
cout << "Number: " << number << endl;
}
};

// Function to sort MyNum objects


void sortMyNums(MyNum nums[], int size) {
for (int i = 0; i < size - 1; ++i) {
for (int j = 0; j < size - i - 1; ++j) {
if (nums[j].getNumber() > nums[j + 1].getNumber()) {
// Swap the objects
MyNum temp = nums[j];
nums[j] = nums[j + 1];
nums[j + 1] = temp;
}
}
}
}
int main() {
// Single MyNum object by taking data from the user
int value;
cout << "Enter a value for a single MyNum object: ";
cin >> value;
MyNum singleNum(value);
cout << "\nSingle MyNum object data:" << endl;
singleNum.display();

// Converting to positive and negative


singleNum.toPositiveNumber();
cout << "Converted to positive: ";
singleNum.display();
singleNum.toNegativeNumber();
cout << "Converted to negative: ";
singleNum.display();

// Creating N MyNum objects


int n;
cout << "\nEnter the number of MyNum objects to create: ";
cin >> n;

MyNum myNums[n];
for (int i = 0; i < n; ++i) {
cout << "Enter value for MyNum object " << i + 1 << ": ";
cin >> value;
myNums[i].setNumber(value);
}
// Displaying N MyNum objects
cout << "\nMyNum objects data:" << endl;
for (int i = 0; i < n; ++i) {
cout << "MyNum object " << i + 1 << ": ";
myNums[i].display();
}

// Sorting the MyNum objects by values


sortMyNums(myNums, n);

// Displaying sorted MyNum objects


cout << "\nSorted MyNum objects data:" << endl;
for (int i = 0; i < n; ++i) {
cout << "MyNum object " << i + 1 << ": ";
myNums[i].display();
}

return 0;
}
Task 3:
#include <iostream>
using namespace std;

class MyChar {
private:
char character;

public:
// Parameterized constructor with default argument
MyChar(char ch = 'a') : character(ch) {}
// Setter for character
void setChar(char ch) {
character = ch;
}

// Getter for character


char getChar() const {
return character;
}

// Convert to uppercase
void toUpperCase() {
if (character >= 'a' && character <= 'z') {
character = character - ('a' - 'A');
}
}

// Convert to lowercase
void toLowerCase() {
if (character >= 'A' && character <= 'Z') {
character = character + ('a' - 'A');
}
}

// Display the character


void display() const {
cout << "Character: " << character << endl;
}
};
// Function to sort MyChar objects by ASCII value
void sortMyChars(MyChar chars[], int size) {
for (int i = 0; i < size - 1; ++i) {
for (int j = 0; j < size - i - 1; ++j) {
if (chars[j].getChar() > chars[j + 1].getChar()) {
// Swap the objects
MyChar temp = chars[j];
chars[j] = chars[j + 1];
chars[j + 1] = temp;
}
}
}
}

int main() {
// Single MyChar object by taking data from the user
char value;
cout << "Enter a character for a single MyChar object: ";
cin >> value;
MyChar singleChar(value);
cout << "\nSingle MyChar object data:" << endl;
singleChar.display();

// Converting to uppercase and lowercase


singleChar.toUpperCase();
cout << "Converted to uppercase: ";
singleChar.display();
singleChar.toLowerCase();
cout << "Converted to lowercase: ";
singleChar.display();

// Creating N MyChar objects


int n;
cout << "\nEnter the number of MyChar objects to create: ";
cin >> n;

MyChar myChars[n];
for (int i = 0; i < n; ++i) {
cout << "Enter character for MyChar object " << i + 1 << ": ";
cin >> value;
myChars[i].setChar(value);
}

// Displaying N MyChar objects


cout << "\nMyChar objects data:" << endl;
for (int i = 0; i < n; ++i) {
cout << "MyChar object " << i + 1 << ": ";
myChars[i].display();
}

// Sorting the MyChar objects by ASCII values


sortMyChars(myChars, n);

// Displaying sorted MyChar objects


cout << "\nSorted MyChar objects data (by ASCII value):" << endl;
for (int i = 0; i < n; ++i) {
cout << "MyChar object " << i + 1 << ": ";
myChars[i].display();
}
return 0;
}

You might also like