[go: up one dir, main page]

0% found this document useful (0 votes)
12 views8 pages

Unit 2, Week 2

Uploaded by

kthecoder5
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)
12 views8 pages

Unit 2, Week 2

Uploaded by

kthecoder5
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/ 8

#include <iostream>

#include <string>

#include <vector>

using namespace std;

// Task 1: Class representing a geometric point in 2D space with multiple constructors

class Point2D {

private:

double x, y;

public:

// Default constructor

Point2D() : x(0.0), y(0.0) {}

// Constructor with x and y coordinates

Point2D(double xCoord, double yCoord) : x(xCoord), y(yCoord) {}

// Copy constructor

Point2D(const Point2D& other) : x(other.x), y(other.y) {}

// Function to display the point

void display() {

cout << "(" << x << ", " << y << ")" << endl;

};

// Task 2: Automated script for tracking stock using Functional Overloading

class StockTracker {

private:

int totalAvailable;
int consumed;

public:

StockTracker(int total) : totalAvailable(total), consumed(0) {}

// Function overloading for tracking consumed items

void trackConsumption(int consumedItems) {

consumed += consumedItems;

// Function overloading for printing remaining stock details

void printRemainingStock(string category) {

cout << "Category: " << category << endl;

cout << "Total Available: " << totalAvailable << endl;

cout << "Consumed: " << consumed << endl;

cout << "Remaining Stock: " << totalAvailable - consumed << endl;

};

// Task 3: Constructor overloading for a class called Person

class Person {

private:

string name;

int age;

string city;

public:

// Constructor with name, age, and city

Person(string n, int a, string c) : name(n), age(a), city(c) {}

// Constructor with name and age


Person(string n, int a) : name(n), age(a), city("") {}

// Constructor with name only

Person(string n) : name(n), age(0), city("") {}

// Function to display person details

void display() {

cout << "Name: " << name << endl;

cout << "Age: " << age << endl;

cout << "City: " << city << endl;

};

// Task 4: Class demonstrating copy constructor and destructor

class StringWrapper {

private:

char* str;

public:

// Constructor

StringWrapper(const char* s) {

int len = strlen(s);

str = new char[len + 1];

strcpy(str, s);

// Copy constructor

StringWrapper(const StringWrapper& other) {

int len = strlen(other.str);

str = new char[len + 1];

strcpy(str, other.str);
}

// Destructor

~StringWrapper() {

delete[] str;

// Function to display the string

void display() {

cout << "String: " << str << endl;

};

// Task 5: Demonstrate copy constructor

void demonstrateCopyConstructor() {

StringWrapper obj1("Hello");

StringWrapper obj2(obj1); // Copy constructor invoked

obj2.display();

// Task 6: Constructor overloading to dynamically allocate memory for an array of integers

class DynamicArray {

private:

int* arr;

int size;

public:

// Default constructor

DynamicArray() : arr(nullptr), size(0) {}

// Constructor with size


DynamicArray(int n) : size(n) {

arr = new int[size];

// Destructor

~DynamicArray() {

delete[] arr;

};

// Task 7: Matrix class with operator overloading

class Matrix {

private:

vector<vector<int>> mat;

public:

// Constructor

Matrix(int rows, int cols) : mat(rows, vector<int>(cols)) {}

// Overload '+' operator for matrix addition

Matrix operator+(const Matrix& other) const {

Matrix result(mat.size(), mat[0].size());

for (int i = 0; i < mat.size(); ++i) {

for (int j = 0; j < mat[0].size(); ++j) {

result.mat[i][j] = mat[i][j] + other.mat[i][j];

return result;

// Overload '-' operator for matrix subtraction


Matrix operator-(const Matrix& other) const {

Matrix result(mat.size(), mat[0].size());

for (int i = 0; i < mat.size(); ++i) {

for (int j = 0; j < mat[0].size(); ++j) {

result.mat[i][j] = mat[i][j] - other.mat[i][j];

return result;

};

// Task 8: Time class

class Time {

private:

int hours, minutes, seconds;

public:

// Constructor

Time(int h, int m, int s) : hours(h), minutes(m), seconds(s) {}

// Overload '+' operator for time addition

Time operator+(const Time& other) const {

int h = hours + other.hours;

int m = minutes + other.minutes;

int s = seconds + other.seconds;

if (s >= 60) {

s -= 60;

m++;

if (m >= 60) {
m -= 60;

h++;

return Time(h, m, s);

// Function to display time

void display() {

cout << "Time: " << hours << ":" << minutes << ":" << seconds << endl;

};

// Task 9: Interaction diagram for a basic web app

// Diagram can be represented in a textual format

int main() {

// Task 1: Test Point2D class

Point2D point1; // Default constructor

Point2D point2(3.5, 2.8); // Constructor with coordinates

Point2D point3(point2); // Copy constructor

cout << "Point1: ";

point1.display();

cout << "Point2: ";

point2.display();

cout << "Point3 (copy of Point2): ";

point3.display();

cout << endl;

// Task 2: Test StockTracker class

StockTracker groceryStock(1000);

StockTracker electronicsStock(500);
groceryStock.trackConsumption(200);

electronicsStock.trackConsumption(100);

cout << "Grocery Stock Details:\n";

groceryStock.printRemainingStock("Grocery");

cout << "Electronics Stock Details:\n";

electronicsStock.printRemainingStock("Electronics");

cout << endl;

return 0;

You might also like