Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions 1-CLASSES-OBJECTS-AND-DATA-STRUCTURES/homework/Order.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#ifndef ORDER_H
#define ORDER_H
#include <string>
#include <vector>
#include "Product.h"
using namespace std;

class Order{
private:
int orderID;
string customerName;
vector<Product*> orderedProducts;

public:
Order(int id, const string& name)
: orderID(id), customerName(name) {}

void addProduct(Product* product) {
orderedProducts.push_back(product);
}
double calculateOrderTotal() const {
double total=0.0;
for(const auto& product : orderedProducts){
total+=product->getProductPrice();
}
return total;
}

int getOrderID() const { return orderID; }
string getCustomerName() const { return customerName; }
const vector<Product*>& getOrderedProducts() const { return orderedProducts; }
};

#endif
22 changes: 22 additions & 0 deletions 1-CLASSES-OBJECTS-AND-DATA-STRUCTURES/homework/Product.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef PRODUCT_H
#define PRODUCT_H

#include <string>
using namespace std;

class Product{
private:
int productID;
string productName;
double productPrice;

public:
Product(int id, const string& name, double price)
: productID(id), productName(name), productPrice(price) {}

int getProductID() const { return productID; }
string getProductName() const { return productName; }
double getProductPrice() const { return productPrice; }
};

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,28 @@ using namespace std;

/* Solution */


class Book{
private:
string title;
string author;
int publicationYear;
string isbn;

public:
void setBookDetails(string t, string a, int year, string i){
title=t;
author=a;
publicationYear=year;
isbn=i;
}

void displayBookDetails(){
cout<<"Title: "<<title<<endl;
cout<<"Author: "<<author<<endl;
cout<<"Publication Year: "<<publicationYear<<endl;
cout<<"ISBN: "<<isbn<<endl;
}
};

int main() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,45 @@ using namespace std;

/* Solution */

class Employee{
private:
int id;
string name;
string designation;
float salary;

public:
Employee(){
id=0;
name="Unknown";
designation="Not Assigned";
salary=0.0;
}

Employee(int empID, string empName, string empDesignation, float empSalary){
id=empID;
name=empName;
designation=empDesignation;
salary=empSalary;
}

void setID(int empID) { id = empID; }
void setName(string empName) { name = empName; }
void setDesignation(string empDesignation) { designation = empDesignation; }
void setSalary(float empSalary) { salary = empSalary; }

int getID() { return id; }
string getName() { return name; }
string getDesignation() { return designation; }
float getSalary() { return salary; }

void displayDetails() {
cout << "ID: " << id << endl;
cout << "Name: " << name << endl;
cout << "Designation: " << designation << endl;
cout << "Salary: $" << salary << endl;
}
};


int main() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,41 @@ class BankAccount {

public:
// Constructor
BankAccount(int accountNumber, const string &holderName, double initialBalance) {
// TODO: Initialize member variables
BankAccount(int accNumber, const string &name, double Abalance) {
accountNumber=accNumber;
holderName=name;
balance=Abalance;
}

void deposit(double amount){
if(amount>0){
balance+=amount;
cout<<"Deposit:"<<amount<<endl;
}
else{
cout<<"Not a deposit amount:"<<endl;
}
}

void withdraw(double amount){
if(amount>0 && amount<=balance){
balance=balance-amount;
cout<<"Withdraw:"<<amount<<endl;
}
else{
cout<<"Error"<<endl;
}
}

double getBalance() const {
return balance;
}

void displayDetails() const {
cout<<"Account Number:"<<accountNumber<<endl;
cout<<"Name:"<<holderName<<endl;
cout<<"Balance:"<<balance<<endl;
}
// Member functions
// TODO: Implement member functions for deposit, withdraw, and check balance
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,57 @@ class Employee {

public:
// TODO: Implement setters and getters
void setName(const string& n){
name=n;
}

void setAge(int a){
age=a;
}

void setPosition(const string& p){
position = p;
}

void setSalary(double s){
salary=s;
}

string getName() const{
return name;
}

int getAge() const{
return age;
}

string getPosition() const{
return position;
}

double getSalary() const{
return salary;
}

};



int main() {

// TODO: Test the Employee class by creating an object, setting attributes, and getting attribute values
Employee emp;

emp.setName("Aysel");
emp.setAge(20);
emp.setPosition("Worker");
emp.setSalary(100);

cout<<"Employee Details:"<<endl;
cout<<"Name: "<<emp.getName()<<endl;
cout<<"Age: "<<emp.getAge()<<endl;
cout<<"Position: "<<emp.getPosition()<<endl;
cout<<"Salary: "<<emp.getSalary()<<endl;
return 0;
}

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <iostream>

#include "Product.h"
#include "Order.h"
using namespace std;

/*
Expand Down Expand Up @@ -65,9 +66,9 @@ int main() {
Order order(1, "John Doe");

// Add products to the order
order.addProduct(p1);
order.addProduct(p2);
order.addProduct(p3);
order.addProduct(&p1);
order.addProduct(&p2);
order.addProduct(&p3);

// Calculate the order total
double total = order.calculateOrderTotal();
Expand All @@ -77,8 +78,8 @@ int main() {
cout << "Customer Name: " << order.getCustomerName() << endl;
cout << "Ordered Products:" << endl;

for (const Product& product : order.getOrderedProducts()) {
cout << " - " << product.getProductName() << endl;
for (const Product* product : order.getOrderedProducts()) {
cout << " - " << product->getProductName() << endl;
}

cout << "Total: $" << total << endl;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@

#include <iostream>

#include <string>
using namespace std;

/*
Exercise:
Create a program to manage a library with books using Object-Oriented Programming (OOP) principles in C++.
Expand Down Expand Up @@ -40,6 +38,29 @@ using namespace std;

class Book {
// TODO:
private:
string title;
string author;
int year;

public:
Book(){
title="";
author="";
year=0;
}

Book(const string& t, const string& a, int y){
title=t;
author=a;
year=y;
}

void display() const {
cout<<"Title:"<<title<<endl;
cout<<"Author:"<<author<<endl;
cout<<"Year:"<<year<<endl;
}
};


Expand All @@ -48,15 +69,28 @@ int main() {
const int librarySize = 5;
Book library[librarySize];

cout << "Enter details for " << librarySize << " books:" << endl;
cout << "Enter details for " << librarySize << " books: " << endl;
for (int i = 0; i < librarySize; ++i) {
// TODO:
string title, author;
int year;
cout<<"Book"<<i + 1<<"title:";
getline(cin, title);

cout<<"Book "<<i + 1<<"author:";
getline(cin, author);

cout<<"Book"<<i + 1<<"year:";
cin >> year;
library[i]=Book(title, author, year);
}

cout << endl << "Library Contents:" << endl;
for (int i = 0; i < librarySize; ++i) {
cout << "Book " << i + 1 << ":" << endl;
// TODO:
library[i].display();
cout << endl;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,53 @@ using namespace std;
Solution
*/
class BankAccount {
}
private:
int accountNumber;
double balance;
static int accountCount;

public:
BankAccount(int accNumber, double initialBalance=0.0){
accountNumber=accNumber;
balance=initialBalance;
accountCount++;
cout<<"BankAccount created. Account Number: "<<accountNumber<<endl;
}

void deposit(double amount){
if(amount>0){
balance+=amount;
cout<<"Deposited "<<amount<<" into account "<<accountNumber<<endl;
}
else{
cout<<"Error"<<endl;
}
}

void withdraw(double amount){
if(amount>0 && amount<=balance){
balance=balance-amount;
cout<<"Withdrew "<<amount<<" from account "<<accountNumber<<endl;
}
else{
cout<<"Error"<<endl;
}
}

double getBalance() const {
return balance;
}

~BankAccount(){
accountCount--;
cout<<"BankAccount closed. Account Number: "<<accountNumber<<endl;
if(accountCount==0){
cout<<"All accounts are now closed. Logging system shutdown."<<endl;
}
}
};

int BankAccount::accountCount=0;


int main() {
Expand Down
Loading