Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <conio.h>
- /* Strings: C++ style */
- #include <string>
- using namespace std;
- class OverflowException {
- public:
- OverflowException() {
- cout << endl << "Exception created!" << endl;
- }
- OverflowException(const OverflowException&) {
- cout << "Exception copied!" << endl;
- }
- ~OverflowException() {
- cout << "Exception finished!" << endl;
- }
- };
- class Vehicle {
- protected:
- char* regnr;
- int razg;
- char* razvalsts;
- public:
- Vehicle();
- Vehicle(char*, int, char*);
- virtual ~Vehicle() {
- cout << "Message from the \"Vehicle\" - destroyed!" <<
- endl;
- }
- char* GetRegnr() const {
- return regnr;
- }
- void SetRegnr(char* regnr) {
- this->regnr = regnr;
- }
- int GetRazg() const;
- void SetRazg(int);
- char* GetRazvalsts() const {
- return razvalsts;
- }
- void SetRazvalsts(char* razvalsts){
- this->razvalsts = razvalsts;
- }
- virtual void Print() const;
- };
- class Car : public Vehicle{
- private:
- int dzineja_jauda;
- public:
- Car():Vehicle(), dzineja_jauda(0){
- }
- Car(char*, int, char*, int);
- virtual ~Car(){
- cout << endl << "Message from the \"Car\" - destroyed!" << endl;
- }
- int GetDzinejajauda() const{
- return dzineja_jauda;
- }
- void SetDzinejajauda(int dzineja_jauda){
- this->dzineja_jauda = dzineja_jauda;
- }
- virtual void Print() const;
- };
- class CarPark {
- private:
- typedef Car* DPPointer;
- DPPointer *Nodes;
- static const unsigned int DEFAULT_MAX_LENGTH;
- unsigned int MaxLength;
- unsigned int Length;
- public:
- CarPark() : MaxLength(DEFAULT_MAX_LENGTH), Length(0) {
- Nodes = new DPPointer[MaxLength];
- }
- CarPark(unsigned int MaxLength) : MaxLength(MaxLength), Length(0) {
- Nodes = new DPPointer[MaxLength];
- }
- ~CarPark();
- static unsigned int GetDefaultMaxLength() {
- return DEFAULT_MAX_LENGTH;
- }
- int GetMaxPower() const {
- int nowpower = 0;
- for (int i = 0; i < Length; i++) {
- if (Nodes[i]->GetDzinejajauda() > nowpower)
- nowpower = Nodes[i]->GetDzinejajauda();
- }
- return nowpower;
- }
- int GetMaxLength() const {
- return MaxLength;
- }
- int GetLength() const {
- return Length;
- }
- void AddNode(const Car&);
- void Print() const;
- };
- const unsigned int CarPark::DEFAULT_MAX_LENGTH = 5;
- Vehicle::Vehicle() : regnr("LV1234"), razg(1998), razvalsts("Latvija") {
- }
- Vehicle::Vehicle(char* Pregnr, int Prazg, char* Prazvalsts) : regnr(Pregnr) {
- razg = Prazg;
- razvalsts = Prazvalsts;
- }
- inline int Vehicle::GetRazg() const {
- return razg;
- }
- inline void Vehicle::SetRazg(int razg) {
- this->razg = razg;
- }
- inline void Vehicle::Print() const {
- cout << "Regnr: " << regnr << ", Razgads: " << razg << ", Razvalsts: " << razvalsts;
- }
- Car::Car(char* Pregnr, int Prazg, char* Prazvalsts, int Pdzineja_jauda) : Vehicle(Pregnr,Prazg,Prazvalsts){
- dzineja_jauda = Pdzineja_jauda;
- }
- inline void Car::Print() const{
- Vehicle::Print();
- cout << ", Dzineja jauda: " << dzineja_jauda;
- }
- CarPark::~CarPark() {
- for(unsigned int i=0; i<Length; i++)
- delete Nodes[i];
- delete [] Nodes;
- }
- void CarPark::Print() const {
- cout << "Nodes:" << endl;
- for (unsigned int i=0; i<Length; i++) {
- cout << (i+1) << ". ";
- Nodes[i]->Print();
- cout << "." << endl;
- }
- }
- void CarPark::AddNode(const Car& Node) {
- if (Length == MaxLength)
- throw OverflowException();
- else
- Nodes[Length++] = new Car(
- Node.GetRegnr(), Node.GetRazg(), Node.GetRazvalsts(), Node.GetDzinejajauda()
- );
- }
- void main(void) {
- CarPark *Park = new CarPark(2);
- Car *D1 = new Car("DE8887", 2005, "Vacija", 150);
- Car D2("FR5252", 2018, "Francija", 207);
- try {
- Park->AddNode(*D1);
- cout << "\nNew node has been added successfully!" << endl;
- }
- catch (const OverflowException&) {
- cout << "*** Error: maximal length exceeded ! ***" << endl;
- }
- catch (...) {
- cout << "Unknown Error!" << endl;
- }
- delete D1;
- cout << "\n\nDefault maximal power (from CLASS): " <<
- CarPark::GetDefaultMaxLength() << "." << endl;
- cout << "Default maximal power (from OBJECT): " <<
- Park->GetDefaultMaxLength() << "." << endl;
- cout << "Maximal power: " << Park->GetMaxLength() << "." << endl;
- cout << "Current power: " << Park->GetLength() << "." << endl;
- try {
- Park->AddNode(D2);
- cout << "\nNew node has been added successfully!" << endl;
- }
- catch (const OverflowException&) {
- cout << "*** Error: maximal length exceeded ! ***" << endl;
- }
- catch (...) {
- cout << "Unknown Error !" << endl;
- }
- try {
- Park->AddNode(D2);
- cout << "\nNew node has been added successfully!" << endl;
- }
- catch (const OverflowException&) {
- cout << "*** Error: maximal length exceeded! ***" << endl;
- }
- catch (...) {
- cout << "Unknown error !" << endl;
- }
- cout << "Maksimala dzineja jauda: " << Park->GetMaxPower() << "." << endl;
- Park->Print();
- delete Park;
- while (kbhit())
- getch();
- getch();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement