Открыть Электронные книги
Категории
Открыть Аудиокниги
Категории
Открыть Журналы
Категории
Открыть Документы
Категории
class Rect {
private:
double height;
double width;
char fillRect;
char perimRect;
public:
Rect(double h = 1, double w = 1) {
height = h;
width = w;
}
void setFillRect(char c) {
fillRect = c;
}
void setPerimRect(char c) {
perimRect = c;
}
void setWidth(double w) {
width = w;
}
void setHeight(double h) {
height = h;
}
double getWidth() const {
return width;
}
double getHeight() const {
return height;
}
double perim() {
return 2 * (width + height);
}
double area() {
return width * height;
}
void draw() {
for (size_t i = 0; i < width; ++i) {
for (size_t j = 0; j < height; ++j) {
if (i == 0 || i == width - 1)
std::cout << perimRect;
else if (j == 0 || j == height - 1)
std::cout << perimRect;
else
std::cout << fillRect;
}
std::cout << std::endl;
}
}
};
}
int main() {
cf::Rect rect(5, 5);
rect.setFillRect('*');
rect.setPerimRect('=');
rect.draw();
return 0;
}
Результаты:
Лабораторная работа №2.
Тема: КОНСТРУКТОРЫ И ДЕСТРУКТОРЫ
3 - вариант
Задание:
Создайте класс box, конструктору которого передаются три
величины типа double, представляющие длины сторон параллелепипеда.
Класс box должен подсчитывать его объем и хранить результат типа
double. Включите функцию-член vol(), которая выводит на экран объем
каждого объекта класса box.
Текст программы:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
class Box {
private:
double v;
public:
Box(double a, double b,
double c) {
v = a * b * c;
}
void vol() {
printf("V = %lf", v);
}};
int main() {
double a, b, c;
do {
printf("Enter a:");
scanf("%lf", &a);
printf("Enter b:");
scanf("%lf", &b);
printf("Enter c:");
scanf("%lf", &c);
Box b(a, b, c);
b.vol();
getchar();
printf("\n");
} while (1);
return 0;
}
Результаты:
Лабораторная работа №3.
Тема: НАСЛЕДОВАНИЕ
3 - вариант
Задание:
Создать класс-родитель точка, его наследника - класс отрезок,
наследников отрезка «квадрат», «параллелограмм».
Текст программы:
#include <stdio.h>
/*
Создать класс-родитель точка, его наследника - класс отрезок,
наследников отрезка «квадрат», «параллелограмм»
*/
#include <iostream>
#include <conio.h>
#include <Windows.h>
void gotoxy(int xpos, int ypos) {
COORD scrn;
HANDLE hOuput = GetStdHandle(STD_OUTPUT_HANDLE);
scrn.X = xpos; scrn.Y = ypos;
SetConsoleCursorPosition(hOuput, scrn);
}
class Dot {
protected:
int x;
int y;
public:
Dot() {
}
Dot(int x, int y) {
setXY(x, y);
}
void setXY(int x, int y) {
this->x = x;
this->y = y;
}
void Draw() {
gotoxy(x, y);
printf(".");
}
void Erars() {
gotoxy(x, y);
printf(" ");
}
};
}
Line(int x, int y, int x2, int y2) {
setXY(x, y, x2, y2);
}
void setXY(int x, int y, int x2, int y2) {
this->x = x;
this->y = y;
this->x2 = x2;
this->y2 = y2;
}
void Draw() {
if (y != y2 && x != x2) {
int k = (y2 - y) / (x2 - x);
int b = (x2 * y - x * y2) / (x2 - x);
}
}
Squad(int x, int y, int x2, int y2, int x3, int y3, int x4, int y4) {
setDots(x, y, x2, y2, x3, y3, x4, y4);
}
void setDots(int x, int y, int x2, int y2, int x3, int y3, int x4, int y4) {
this->x = x;
this->y = y;
this->x2 = x2;
this->y2 = y2;
this->x3 = x3;
this->y3 = y3;
this->x4 = x4;
this->y4 = y4;
}
void Draw() {
printf("Draw Squad");
}
void Erarse() {
printf("Erarse Squad");
}
void Rotate() {
printf("Rotate Squad");
}
void Fill() {
printf("Fill Squad");
}
};
}
Parallel(int x, int y, int x2, int y2, int x3, int y3, int x4, int y4) {
setDots(x, y, x2, y2, x3, y3, x4, y4);
}
void setDots(int x, int y, int x2, int y2, int x3, int y3, int x4, int y4) {
this->x = x;
this->y = y;
this->x2 = x2;
this->y2 = y2;
this->x3 = x3;
this->y3 = y3;
this->x4 = x4;
this->y4 = y4;
}
void Draw() {
printf("Draw Parallel");
}
void Erarse() {
printf("Erarse Parallel");
}
void Rotate() {
printf("Rotate Parallel");
}
void Fill() {
printf("Fill Parallel");
}
};
SMALL_RECT Rect;
Rect.Top = 0;
Rect.Left = 0;
Rect.Bottom = Height - 1;
Rect.Right = Width - 1;
int main() {
SetWindow(110, 41);
Dot dot(1, 1);
dot.Draw();
dot.setXY(1, 2);
dot.Draw();
dot.Erars();
_getch();
}
Результаты:
Лабораторная работа №4.
Тема: ПОЛИМОРФИЗМ. ПЕРЕГРУЗКА ОПЕРАЦИЙ И ФУНКЦИЙ
3 - вариант
Задание:
В британском формате дата задается как число/месяц/год. Реализовать с
учетом високосных годов:
а) сложение даты с заданным количеством дней (операция +);
б) вычитание из даты заданного количества дней (операция -).
Текст программы:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <locale.h>
#include <iostream>
class date
{
private:
int day, month, year;
bool bChangeDay, bChangeMonth, bChangeYear;//Флаги указывающие на корректировку
char sDate[256];//Дата в строковом формате
public:
date(); //конструктор без параметров
date(int iDay, int iMonth, int iYear);//конструктор с параметрами
date(const date& val); //конструктор копирования
~date(); //деструктор
return a;
}
date::date()
{
day = 1;
month = 1;
year = 1900;
bChangeDay = false;
bChangeMonth = false;
bChangeYear = false;
sDate[0] = '\0';
}
sDate[0] = '\0';
}
sDate[0] = '\0';
}
date::~date()
{
//Память не выделяли так что думаю,
//кроме установки дефалтных значений
//ничего сделать и не можем
day = 1;
month = 1;
year = 1900;
sDate[0] = '\0';
}
int main() {
setlocale(LC_ALL, "");
date a(20, 12, 1899);
printf("%s", a.printf(0));
printf("%s - оператор - \n", (a-5).printf(0));
printf("%s - оператор + ", (a+5).printf(0));
return 0;
}
Результаты: