We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3
#include <iostream> return true; // Company Class
#include <string> } class Company {
using namespace std; return false; private: class Resource { } Employee** employees; public: bool RemoveResource(const Resource& resource) { int employeeCount; int ID; for (int i = 0; i < resourceCount; ++i) { const sta c int MAX_EMPLOYEES = 50; Resource(int id = 0) : ID(id) {} if (resources[i].ID == resource.ID) { public: }; for (int j = i; j < resourceCount - 1; ++j) { Company() : employeeCount(0) { class Employee { resources[j] = resources[j + 1]; employees = new Employee * protected: } [MAX_EMPLOYEES]; string Name; resourceCount--; } int Salary; return true; Employee** GetEmps() const { int ID; } return employees; public: } } Employee(string name, int salary, int id) : return false; void SetEmps(Employee** emps, int count) { Name(name), Salary(salary), ID(id) {} } employeeCount = count; virtual void ChangeSalary(int salary) { Salary = void CalculateRaise() override { for (int i = 0; i < count; ++i) { salary; } Salary += sta c_cast<int>(Salary * 0.10); employees[i] = emps[i]; virtual void Print() = 0; } } virtual void CalculateRaise() = 0; ~Developer() { } virtual ~Employee() {} delete[] resources; void Print() const { }; } for (int i = 0; i < employeeCount; ++i) { class Manager : public Employee { }; employees[i]->Print(); private: class Tester : public Employee { } int Rank; private: } public: string Tes ngType; ~Company() { Manager(string name, int salary, int id, int rank) Resource* resources; for (int i = 0; i < employeeCount; ++i) { : Employee(name, salary, id), Rank(rank) {} int resourceCount; delete employees[i]; const sta c int MAX_RESOURCES = 10; } void Print() public: delete[] employees; { Tester(string name, int salary, int id, string type) } cout << "Manager Name: " << Name << ", Salary: : Employee(name, salary, id), Tes ngType(type), }; " << Salary; resourceCount(0) { cout << ", ID: " << ID << ", Rank: " << Rank << resources = new Resource[MAX_RESOURCES]; // Global func on to print employee info endl; } void PrintEmployeeInfo(Employee* emp) { } void Print() { emp->Print(); void ChangeSalary(int salary) override { cout << "Tester Name: " << Name << ", Salary: " } Salary = salary; << Salary; } cout << ", ID: " << ID << ", Tes ng Type: " << int main() void IncreaseRank() { Tes ngType << endl; { Rank++; cout << "Resources: "; // Define array of 3 resources and read their Ids } for (int i = 0; i < resourceCount; ++i) { from the user void CalculateRaise() override { cout << resources[i].ID << " "; Resource resources[3]; Salary += sta c_cast<int>(Salary * 0.15); } for (int i = 0; i < 3; ++i) { } cout << endl; int id; }; } cout << "Enter Resource ID: "; class Developer : public Employee { void ChangeSalary(int salary) override { cin >> id; private: Salary = salary; resources[i] = Resource(id); string CodingLanguage; } } Resource* resources; Resource* GetResources() const { // Define a developer with the resources array int resourceCount; return resources; defined above const sta c int MAX_RESOURCES = 10; } Developer dev("Smith Clark", 3000, 123, "C++"); public: bool AddResource(const Resource& resource) { for (int i = 0; i < 3; ++i) { Developer(string name, int salary, int id, string if (resourceCount < MAX_RESOURCES) { dev.AddResource(resources[i]); language) resources[resourceCount++] = resource; } : Employee(name, salary, id), return true; // Add a resource to the developer defined above CodingLanguage(language), resourceCount(0) { } with ID=77 resources = new Resource[MAX_RESOURCES]; return false; Resource newResource(77); } } dev.AddResource(newResource); void Print(){ bool RemoveResource(const Resource& resource) { // Remove the added resource above cout << "Developer Name: " << Name << ", for (int i = 0; i < resourceCount; ++i) { dev.RemoveResource(newResource); Salary: " << Salary; if (resources[i].ID == resource.ID) { // Print only the resources array of the defined cout << ", ID: " << ID << ", Language: " << for (int j = i; j < resourceCount - 1; ++j) { developer CodingLanguage << endl; resources[j] = resources[j + 1]; cout << "Developer's Resources: "; cout << "Resources: "; } for (int i = 0; i < 3; ++i) { for (int i = 0; i < resourceCount; ++i) { resourceCount--; cout << dev.GetResources()[i].ID << " "; cout << resources[i].ID << " "; return true; } } } cout << endl; cout << endl; } // Print all the info of the developer using the } return false; global func on void ChangeSalary(int salary) override { } PrintEmployeeInfo(&dev); Salary = salary; void CalculateRaise() override { return 0; } Salary += sta c_cast<int>(Salary * 0.07); } Resource* GetResources() const { } return resources; ~Tester() { } delete[] resources; bool AddResource(const Resource& resource) { } if (resourceCount < MAX_RESOURCES) { }; resources[resourceCount++] = resource; #include <iostream> // Global func on to print array of pointers of class Array{ using namespace std; Vehicles private: #define MAX 100 void printVehicleList (VehicleList& vlist, int n) int* p; // pointer to an array of integers // Abstract Base class Vehicle { int size; // size of the array class Vehicle { Vehicle** vArray vlist Getlist(); public: public: for (int i = 0; i < n; i++) // Constructor virtual void se d (int)= 0; { Array (int s) : size(s) virtual int ge d ()= 0; VArray[i]->print(); { virtual void print() <= 0; cout << endl; p = new int[size]; }; } for (int i = 0; i < size; i++) // Derived class Car } p[i] = 0; // Ini alize array with zeroes class Car : public Vehicle { int main() } private: { void setArray (int* values) int id // Crea ng Car and Bus objects { int model; Car car1 (1, 2024, 250, 2000); for (int i = 0; i < size; i++) int hp; Car car2 (2, 2022, 350, 3000); p[i]= values [i]; int engSize; Bus bus1 (3, 2023, 50, 20); } public: Bus bus2 (4, 2021, 60, 22); // Overloading assignment operator Car (int i=0, int m = 0, int h= 0, int e = 0) : id (i), // Array of Pointers to the Base class Vehicle used to void operator= (Array& A) model(m), hp (h), engSize (e) {} store the addresses of Car objects and Bus objects { void se d (int i) { id=i; } Vehicle * vehicles[] = {&car1, &car2, &bus1, &bus2 }; // Deallocate exis ng memory int ge d () { return id; } // Crea ng VehicleList object delete[] p; void print() Vehiclelist vlist (vehicles, 4); // Allocate new memory and copy data { // Prin ng the vehicle list size=A. size; cout<<"Car ID: " << id <<< endl; printVehicleList(vlist, 4); p = new int[size]; cout<<"Car Model: " << model << endl; // Searching for a vehicle int for (int i = 0; i < size; i++) cout<<"Engine hp: " << hp << endl; index=vlist.search(&car2); if (index != -1) p[i]=A.p[i]; cout<<"Engine size: " << engSize << endl; cout<<"Vehicle found at index: " <<index<<endl; } } else void print () const bool operator>(Car& c) cout << "Vehicle not found" << endl; { { // Comparing two cars for (int i = 0; i < size; i++) return (engSize > c.engSize && hp > c.hp); if( car1 > car2 ) cout<< p[i] << “ “<<endl; } cout<<"Car1 is greater than Car2" << endl; } }; else // Destructor // Derived class Bus cout<<"Car2 is greater than or equal to Car1" << endl; ~Array () class Bus : public Vehicle { return 0; { private: } delete[] p; int id; Implemen ng the operator overloading func on } int model; using a friend func on: }; int numofseats; // Func on. prototype int wheelSize; friend bool operator> (Car& c1, Car& c2); int main() public: // Func on defini on } Bus (int i=0, int m = 0, int n = 0, int w = 0): id(i), bool operator> (Car& c1, Car& c2) // Crea ng an Array object model(m), numofseats (n), wheelSize (w) {} { Array arr1 (5); void se d (int i) { id=i; } return (c1.engSize > c2.engsize && c1.hp > c2.hp); cout<<"arr1 = "; int ge d () { return id; } } arr1.print(); void print() int a[] ={4, 2, 3, 1, 8} } // Se ng arr1 with the values of array a cout<<"Bus ID: " << id << endl; arr1.setArray (a); cout<<"3us Model: " << model <<< endl; cout<<"arr1 = "; cout<<"Number of seats: " << numofseats << endl; arr1.print(); cout << "Wheel size: " << wheelSize << endl; // Crea ng another Array object } Array arr2 (3); }; cout << "arr2 = "; // Class VehicleList arr2.print(); class VehicleList { int b[] = { 9, 7, 6 }; Vehicle * vp [MAX]; // Se ng arr2 with the values of array b int n; // actual number of vehicles arr2.setArray (b); public: //////////////////////////////////////// cout<<"arr2 = "; VehicleList (Vehicle* p[], int c) n(c) //Excep on Handling arr2.print(); { int main() { //Assigning arr1 to arr2 for (int i = 0; i < c; i++) try{ arr2 = arr1; vp[i] = p[i]; int a,b; cout<<”arr2=”; } cin>>a>>b; arr2.print(); Vehicle** Getlist() if (b == 0) throw excep on ("Zero"); return 0; } } } return vp; catch (excep on e) { // return the address of the array of pointers e.what(); } } int search (Vehicle* v) } { /////////////////////////////////////// for (int i = 0; i < n; i++) if (vp[i]->ge d () == v->ge d()) return i; return -1; // vehicle Not found } };
#include <iostream> using namespace std;
//Template Example is defined in the base class and the other in the happens destroying automa c objects/variables and #include <iostream> derived calling the destructors and returning to previous using namespace std; class. func on un l try/catch is found (if not found, template <class T, class U = char> Examples of overloading: terminates). class A int sum (int a, int b); //original func on Destructors of the objects (whose scope ends with { int sum (float a, float b) the try block is automa cally called before the catch T x; int sum (int a, int b, int c); block gets executed). U y; float sum (int a, int b) X compila on error When constructor of an object throws an excep on, template<typename T> /////////////////////////////////////////////// destructor for the object is not called as object is not class Base { Downcas ng: base-object’s address to derived-class fully constructed. private: pointer (called downcas ng ), this allows derived If catch can handle base class, it can handle all the T x; class func onality derived classes. This means if there is catch that public: ((Circle*) Point_ptr) ->getarea ( ); catches base class, followed by another catch to catch }; derived class, and derived class is thrown, the base A( T a, U b) : x (a), y(b) {} Upcas ng: (could be unsafe) derived class pointer to class catch will be the on executed as it comes before T getx() { return x; } base class pointer. In this case the derived class that derived one. To catch ANY type of excep on use: U gety() { return y; }. pointer can only access base class members. catch(..) int main() ((Point* ) Circle_ptr) -> print( ); } A<char> 01 ('b', 'd') Dynamic_cast<DerivedClass*>(BasePointer) cout<<"="<<01.getX () <<" y="<<01.getY () <<endi Returns NULL if it cant down cast, if it can it returns A<int double> 02 (10, 6.4) derived class pointer cout<<" = "<<02.getX () <<" y="<<02.getY()<<endl; ////////////////////////////////////////////// return 0; Base class pointer can point to derived class but not } the other way around(compile error) HOWEVER it Output: calls base class versions of the func ons on derived x=b y=d class (unless it is virtual) x= 10 y= 6.4 Derived member func ons cannot be called using ////////////////////////////////////////////////// base class pointer to derived class Passing variable to func on: ////////////////////////////////////////////// Pass by value: void func on(int x) A class that includes a pure virtual func on is an //cant change value of x Abstract Class! No objects can be instan ated from in main //call: func on(x); an abstract class!! But pointers can be declared to Pass by pointer: void func on(int*p) them! Virtual func ons may be redefined (but not //passes pointer necessarily)! //poin ng to value to be changed changes value However, Pure virtual func ons MUST be redefined! in main //call: func on(&x); Otherwise the derived class is also abstract! Pass by reference: void func on(int&p) ////////////////////////////////////////////// //call: func on(x); Sta c members/func ons: exist independent of an //creates an alias ‘p’ to x same loca on changes value object. of x in main Can be accessed by any obj of the class or //////////////////////////////////////////////// classname::membername (even when no object #pragma once //prevents mul ple inclusions of exists) header file Members: //sta c int count; belong to the class as a #include “filename.h” //included in every file that whole and NOT to a specific object of the class. uses class not wri en in actual header file Can be accessed through any member func on /////////////////////////////////////////////// (including OUTSIDE class: If private, then they are Const data members: ini alizer list must be used for accessed through any public member func on const data members, can be used for non const If public, can be accessed through class name and /////////////////////////////////////////////// scope Resolu on sta c and non-sta c member Order of construc on: class members, outer class func ons) constructor Func ons: //sta c void getCount; Order of destruc on: outer class destructor, class cannot access non-sta c data or func ons (No this members from bo om to top pointer for sta c func ons) can only access: /////////////////////////////////////////////// sta c data member, other sta c member func ons Constructor hierarchy: and any other func ons from outside the class. When derived class object is created, base class of private sta c variables can only be accessed via public inheritance is constructed first (base constructor is member func ons (even when no class member invoked implicitly or explicitly) followed by next objects exist) derived class in hierarchy. Sta c data members are ini alized outside Destructor hierarchy: class defini on Reverse order of constructor chain int Employee :: count = 0; Destructor of derived class called first, then next base ////////////////////////////////////////////// class up hierarchy and so on un l final base Operator overloading: Object is only removed from memory a er final base . .* :: ?: can’t be overloaded class destructor. ////////////////////////////////////////////// ////////////////////////////////////////////// Prefix: Overloading: two or more func ons have the same c1=++c2; implementa on : Counter operator ++ ( ) { name count++; Return Counter (count);} but different parameters. Return type can be similar Pos ix: or same but add between brackets int and return by different. (changing only the return type is value ex:Counter Counter :: operator++( int ) { int val Compila on = count; count ++; return Counter ( val); } ERROR) By different parameters we mean: number, order and type of parameters Overriding: same func on name, same parameters list and same return type. One Stack unwinding: if an excep on is not handled in the func on same func on where it is thrown, stack unwinding