Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Звіт_ООП.docx
Скачиваний:
0
Добавлен:
01.03.2025
Размер:
586.36 Кб
Скачать

Int main(int argc, char* argv[])

{

d o1(5),o2(5);

for (int i = 0; i < o1.getN ();i++)

{

o1[i] = (i-1)*3;

o2[i] = 2+i;

}

cout<<"\nol :\t\t";

view (o1);

cout<<"o2:\t\t";

view (o2);

d res ;

res = o1 + o2 ;

cout<<"\nres = ol + o2:\t";

view (res);

int x(5);

d o3(5);

cout<<"\no3:\t\t";

view (o3);

res = o3 + x ;

cout<<"res = оЗ + 5:\t";

view (res);

res = x + o3;

cout<<"res = 5 + o3:\t" ;

view (res);

res = o3 - o2;

cout<<"\nres = оЗ - o2:\t";

view ( res);

res = o2 - o3;

cout<<"res = o2 - o3 :\t";

view (res );

// вывод состояния исходных объектов

cout<<"\nol :\t\t";

view (o1);

cout<<"o2:\t\t";

view (o2);

cout<<"o3 :\t\t";

view (o3 );

system("pause");

return 0;

}

// функция вывода состояния объектов типа С

void view (const d& o)

{

for (int і = 0; і < o.getN(); і++)

cout<< o[ і ] << "\t";

cout<< endl;

}

d operator + (const d& left, const d& right)

{

d tmp (left.m_n);

for (int i = 0; i < tmp.m_n; i++)

tmp [ i ] = left [ i ] + right [ i ];

return tmp;

}

d operator + (const d& left, const int& right)

{

d tmp (left.m_n );

for (int і = 0; і < tmp.m_n; і++)

tmp [ і ] = left [ і ] + right;

return tmp;

}

d operator + (const int& left, const d& right)

{

d tmp (right.m_n);

for (int і =0; і < tmp.m_n ; і++)

tmp [ і ] = left + right [ і ];

return tmp;

}

d operator - (const d& left, const d& right)

{

d tmp (left.m_n);

for (int і = 0; і < tmp.m_n; і++)

tmp[і] = left[і]-right[і];

return tmp;

}

Копія вікна виконання програми

Висновок: Під час виконання лабораторної роботи я навчився проектувати та створювати програми з перевантаженням операторів за допомогою дружніх функцій.

Лабораторна робота № 15-16

Проектування та створення програми з простим успадкуванням класів

Тема: навчити проектувати та створювати програми з простим успадкуванням класів.

Хід роботи

  1. Запустив Microsoft Visual C++ та створив проект типу Console application.

  2. Створив базовий клас basis

  3. До файлу специфікації basis.h додав наступний код

#pragma once

class basis

{

public:

int a,b;

friend class inherit;

basis();

basis(int,int);

virtual ~basis();

int sum();

int razn();

void getdata();

}

  1. До файлу специфікації basis.cpp додав наступний код

#include "StdAfx.h"

#include "basis.h"

#include <iostream>

using namespace std;

basis::basis()

{}

basis::basis(int x, int y): a(x), b(y)

{

cout << "Basis a= " << a <<" b= " << b << endl;

}

basis::~basis()

{}

int basis::sum()

{

return a+b;

}

int basis::razn()

{return a-b;}

void basis::getdata()

{

cout<<endl<<a<<endl<<b;

}

  1. Створив похідний клас inherit

  2. До файлу специфікації inherit.h додав наступний код

#pragma once

#include "basis.h"

class inherit:public basis

{

public:

int mult;

float div;

int proc;

inherit();

inherit(int a_bas,int b_bas,int pr_int);

virtual ~inherit();

void friend view(inherit&);

void set_mult();

void set_div();

void view();

};

  1. До файлу специфікації inherit.cpp додав наступний код

#include "StdAfx.h"

#include "inherit.h"

#include <iostream>

using namespace std;

inherit::inherit()

{

}

inherit::~inherit()

{

}

inherit::inherit(int a_bas, int b_bas, int pr_in): proc(pr_in), basis(a_bas,b_bas)

{

cout << "Inherit a_bas= " << a_bas <<" b_bas= " << b_bas << endl;

cout << "Inherit a= " << a <<" b= " << b << endl;

}

void inherit::set_mult()

{

cout << "Inherit Set_mult: a= " << a << " b= "<< b<<endl;

if ((a==0) || (b==0))

mult = 0;

else

mult = a*b;

cout << "Inherit mult= " << mult << endl;

}

void inherit::set_div()

{

if (b != 0)

{

div = a/b;

cout<<"Inherit div= " << div << endl;

}

else

cout<<"Inherit div ne obchislene "<<endl;

}

void inherit::view()

{

cout << "ob a= " <<a << " b= " << b << " proc= " << proc << endl;

}

  1. Головна функція програми:

#include "stdafx.h"

#include "inherit.h"

#include <iostream>

using namespace std;

void view (inherit& ob)

{

cout<<" ob a = "<<ob.a<<" ob b= "<<ob.b<<" proc = "<<ob.proc<<endl;

}

int _tmain(int argc, _TCHAR* argv[])

{

inherit ob1(5,6,20);

int summa = ob1.sum();

int razn = ob1.razn();

cout<<" summa = "<<summa<<" razn = "<<razn<<endl;

view(ob1);

ob1.set_div();

ob1.set_mult();

system("pause");

return 0;

}

Копія вікна виконання програми

Висновок: Під час виконання лабораторної роботи я навчився проектувати та створювати програми з простим успадкуванням класів, створювати базові та класи потомки.

Лабораторна робота № 17-18

Проектування та створення програми побудови ієрархії класів

Мета: навчитись будувати ієрархії класів в програмах.

Хід роботи

1.Запустив Microsoft Visual C++ та створив проект типу Console application.

2.Створив базовий клас CCar

#pragma once

#include <string>

using namespace std;

class CCar

{

protected:

string m_model;

double m_mass;

public:

CCar();

CCar(const string&, const double);

CCar (const CCar&);

virtual void mod(int );

friend class Cmenu;

virtual ~CCar();

};

#include "car.h"

CCar::CCar ():m_model(""),m_mass(0.0){}

CCar::~CCar (){}

CCar::CCar(const string& md,const double m):m_model(md),m_mass(m){}

CCar::CCar(const CCar& c):m_model(c.m_model),m_mass(c.m_mass){}

void CCar::mod (int n)

{

m_mass+=n;

}

3.Створив базовий клас CCargo

#pragma once

#include "car.h"

class CCargo:public CCar

{

protected:

int m_carCapacity;

public:

CCargo();

CCargo(const string&, const double,const int);

CCargo (const CCargo&);

virtual void mod(int);

friend class Cmenu;

virtual ~CCargo();

};

#include "cargo.h"

CCargo::CCargo():CCar(),m_carCapacity(0){}

CCargo::~CCargo() {}

CCargo::CCargo(const string& md, const double m, const int c):CCar(md,m),m_carCapacity(c) {}

CCargo::CCargo(const CCargo& c): CCar(c), m_carCapacity(c.m_carCapacity){}

void CCargo::mod (int n)

{

CCar::mod (n);

m_carCapacity+=n;

}

4.Створив базовий клас CTank

#pragma once

#include "cargo.h"

class CTank:public CCargo

{

double m_volume;

public:

CTank();

CTank(const string&, const double, const int,const double);

CTank( const CTank&);

virtual ~CTank();

void setvolume(const double);

double getvolume() const;

void mod(int );

friend class Cmenu;

};

#include "tank.h"

CTank::CTank():CCargo(),m_volume(0.0) {}

CTank::~CTank() {}

CTank::CTank(const string& md, const double m, const int c, const double v): CCargo(md,m,c),m_volume(v){}

CTank::CTank(const CTank& c):CCargo(c), m_volume(c.m_volume){}

void CTank::setvolume(const double v){m_volume=v;}

double CTank::getvolume() const {return m_volume;}

void CTank::mod (int n)

{ m_volume+=n;}

5.Створив базовий клас CPlatform

#pragma once

#include "cargo.h"

class CPlatform :public CCargo

{

public:

int m_boards;

CPlatform();

CPlatform(const string&,const double, const int ,const int);

CPlatform(const CPlatform&);

virtual ~CPlatform();

void mod(int );

friend class Cmenu;

};

#include "platform.h"

CPlatform::CPlatform():CCargo(),m_boards(0){}

CPlatform::~CPlatform() {}

CPlatform::CPlatform(const string& md,const double m,const int c,const int b):CCargo (md,m,c),m_boards(b){}

CPlatform::CPlatform(const CPlatform& c):CCargo(c),m_boards(c.m_boards){}

void CPlatform::mod (int n)

{

m_boards-=n;

}

6.Створив базовий клас Ccarriage

#pragma once

#include "car.h"

class CCariage:public CCar

{

string m_type;

int m_places;

public:

CCariage();

CCariage(const string&, const double,const string&,const int);

CCariage(const CCariage&);

virtual ~CCariage();

void settype(const string&);

void setplaces(const int);

string gettype() const;

int getplaces() const;

void mod(int );

friend class Cmenu;

};

#include "carriage.h"

CCariage::CCariage():CCar(),m_type(""),m_places(0){}

CCariage::~CCariage() {}

CCariage::CCariage(const string& md,const double m, const string& t, const int p):CCar(md, m),m_type(t),m_places(p){}

CCariage::CCariage(const CCariage& c):CCar(c),m_type(c.m_type),m_places(c.m_places){}

void CCariage::settype(const string& t) {m_type=t;}

void CCariage::setplaces(const int p) {m_places=p;}

string CCariage::gettype() const {return m_type;}

int CCariage::getplaces()const {return m_places;}

void CCariage::mod (int n)

{

CCar::mod (n);

++m_places;

}

7.Створив базовий клас Cmenu

#pragma once

#include "car.h"

class Cmenu

{

public:

CCar* m_p;

void showMenuAndSelect();

private:

void tank();

void platform();

void carriage();

};

#include <iostream>

using namespace std;

#include "menu.h"

#include "tank.h"

#include "platform.h"

#include "carriage.h"

void Cmenu::showMenuAndSelect()

{

char choice;

do

{

cout<<"\n1. Tank\t2. Platform\t3. Carriage\t0. Exit\n";

cout<<"Please, your choice ->";

cin>>choice;

switch(choice)

{

case '1':tank(); break;

case '2':platform(); break;

case '3':carriage(); break;

}

}

while (choice!='0');

}

void Cmenu::tank()

{

cout<<endl<<"Tank"<<endl;

CTank* p= new CTank("15-1547", 24.5,68,85);

cout<<"Model\t"<<p->m_model<<endl;

cout<<"Mass\t"<<p->m_mass<<endl;

cout<<"Carriage capacity\t"<<p->m_carCapacity<<endl;

cout<<"Volume the boile\t"<<p->m_volume<<endl;

cout<<"\nTANK UPDATING\n";

m_p=p;

m_p->mod(2);

cout<<"Volume of the boiler\t"<<p->m_volume<<endl;

p->mod(2);

cout<<"Volume of the boiler\t"<<p->m_volume<<endl;

CTank o(*p);

m_p=&o;

m_p->mod(4);

cout<<"Volume of the boiler\t"<<o.m_volume<<endl;

delete p;

}

void Cmenu ::platform()

{

cout<<endl<<"Platform"<<endl;

CPlatform* p= new CPlatform ("13-14012", 21,4, 71.4);

cout<<"Model\t"<<p->m_model<<endl;

cout<<"Mass\t"<<p->m_mass<<endl;

cout<<"Carriage capacity\t"<<p->m_carCapacity<<endl;

cout<<"Quantity of boards\t"<<p->m_boards<<endl;

cout<<endl<<"MODIFY PLATFORM"<<endl;

m_p=p;

m_p->mod(2);

cout<<"Quantity of boards\t"<<p->m_boards<<endl;

delete p;

}

void Cmenu::carriage()

{

cout<<endl<<"Carriage"<<endl;

CCariage* p= new CCariage("61-807",55,"Compatrment", 36);

cout<<"Model\t"<<p->m_model<<endl;

cout<<"Mass\t"<<p->m_mass<<endl;

cout<<"Type\t"<<p->m_type<<endl;

cout<<"Number of places\t"<<p->m_places<<endl;

cout<<endl<<"MODIFY CARRIAGE"<<endl;

m_p=p;

m_p->mod(2);

cout<<"Mass\t"<<p->m_mass<<endl;

cout<<"Number of places\t"<<p->m_places<<endl;

delete p;

}

8.Головна функція

#include "menu.h"

int main()

{

Cmenu* p=new Cmenu;

p->showMenuAndSelect();

delete p;

return 0;

}

Копія вікна виконання програми

Висновок: Під час виконання лабораторної роботи я навчився будувати ієрархії класів в програмах.