1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
|
#include <cassert>
#include <string>
#include <iostream>
#include <string.h>
using namespace std;
class Glasses
{
protected:
char * color;
int n_o_l;
public:
Glasses()
{
color = new char[strlen("Black") + 1];
assert(color != 0);
strcpy(color, "Black");
n_o_l = 2;
cout << "so simple Glasses connstructor \n";
}
Glasses(char *col, int number)
{
color = new char[strlen(col) + 1];
assert(color != 0);
strcpy(color, col);
n_o_l = number;
cout << "Glasses constructor\n";
}
Glasses(const Glasses &g)
{
//bad row
color = g.color;
//fix
color = new char[strlen(g.color) + 1];
assert(color != 0);
strcpy(color, g.color);
n_o_l = g.n_o_l;
cout << "Another Glasses constructor \n";
}
void print()
{
cout << "so beautiful" << color << "color glasses with " << n_o_l << " lenses\n";
}
~Glasses()
{
delete[] color;
cout << "Someone stepped over my glassess :(\n";
}
};
class SunGlasses : public Glasses
{
protected:
double howDark;
public:
SunGlasses()
{
howDark = 2.5;
cout << "so simple SunGlasses connstructor \n";
}
SunGlasses(char *col, int number, double dark) : Glasses(col, number)
{
howDark = dark;
cout << "SunGalsses constructor\n";
}
void setHowDark(double dark)
{
if (dark<0)
throw "can't change the darkness \n";
else
howDark = dark;
}
void print()
{
cout << "so beautiful " << color << " color glasses with " << n_o_l << " lenses, and so dark " << howDark << "\n";
}
~SunGlasses()
{
cout << "someone stopped over my SunGlassess :(\n";
}
};
class MovieGlasses : public Glasses
{
int dimension;
public:
MovieGlasses()
{
dimension = 3;
cout << "So simple movieGlasses constructor\n";
}
MovieGlasses(char *color, int number, int dim) : Glasses(color, number)
{
dimension = dim;
cout << "movieglasses constructor\n";
}
void print()
{
cout << "so beautiful " << color << " color glasses with " << n_o_l << " lenses, and " << dimension << " dimension\n";
}
~MovieGlasses()
{
cout << "someone stopped over my movieglasses :(\n";
}
};
int main()
{
Glasses a("red", 5);
SunGlasses b("blue", 6, 1.5);
MovieGlasses *c = new MovieGlasses("pink", 4, 6);
assert(c != 0);
b.print();
c->print();
system("pause");
return 0;
}
|