Skip to content
Snippets Groups Projects
Commit 0257f366 authored by Jake Feddersen's avatar Jake Feddersen
Browse files

4_03 notes

parent e982463a
No related branches found
No related tags found
No related merge requests found
File added
#include <iostream>
using namespace std;
class A {
public:
A() { cout << "Constructing an A" << endl; }
A(const A &a) { cout << "Copying an A" << endl; }
virtual ~A() { cout << "Destroying an A" << endl; }
virtual void print() { cout << "A" << endl; }
};
class B : public A {
public:
B() { cout << "Constructing an B" << endl; }
B(const B &b) { cout << "Copying an B" << endl; }
virtual ~B() { cout << "Destroying an B" << endl; }
virtual void print() { cout << "B" << endl; }
};
class C : public A {
public:
C() { cout << "Constructing an C" << endl; }
C(const C &c) { cout << "Copying an C" << endl; }
virtual ~C() { cout << "Destroying an C" << endl; }
virtual void print() { cout << "C" << endl; }
};
class D : public B, public C {
public:
D() { cout << "Constructing an D" << endl; }
D(const D &d) { cout << "Copying an D" << endl; }
virtual ~D() { cout << "Destroying an D" << endl; }
virtual void print() { cout << "D" << endl; }
};
int main(int argc, char *argv[]) {
return 0;
}
File added
#include <iostream>
#include <cstdlib>
using namespace std;
/*
Polymorphism in c++
Only works with references and pointers!
Cannot get polymorphic behavior with instances
I.E. Impossible to assign an instance of subclass to an instance of type superclass
*/
class shape {
public:
int color;
virtual double area() = 0;
virtual double perimeter() = 0;
virtual ~shape() {};
friend ostream &operator<<(ostream &o, const shape &s);
virtual ostream &print(ostream &o) const = 0;
};
ostream &operator<<(ostream &o, const shape &s) {
return s.print(o);
}
class rectangle : public shape {
protected:
double width, height;
public:
double area() {
return width * height;
}
double perimeter() {
return 2 *(width + height);
}
rectangle() {
width = 1;
height = 2;
}
rectangle(double width, double height) {
this->width = width;
this->height = height;
}
ostream &print(ostream &o) const {
return o << "Rectangle: Width " << width << ", Height " << height << endl;
}
};
/*
class square : public shape {
private:
double side;
public:
double area() {
return side * side;
}
double perimeter() {
return 4 * side;
}
square() {
side = 1;
}
square(double side) {
this->side = side;
}
ostream &print(ostream &o) const {
return o << "Square: Side " << side << endl;
}
};
*/
class square : public rectangle {
private:
public:
square() : rectangle(1, 1) { }
square(double side) : rectangle(side, side) { }
ostream &print(ostream &o) const {
return o << "Square: Side " << width << endl;
}
};
int main(int argc, char *argv[]) {
square s(4);
rectangle r(3, 5);
shape *sp = &s;
shape &sr = r;
shape *a[10];
cout << s << r << endl;
cout << sp->area() << " " << sp->perimeter() << " "
<< sr.area() << " " << sr.perimeter() << endl;
for (int i = 0; i < 10; i++) {
if (rand() % 2) {
a[i] = new square(1 + rand() % 10);
} else {
a[i] = new rectangle(1 + rand() % 10, 1 + rand() % 10);
}
}
for (int i = 0; i < 10; i++) {
cout << *a[i];
}
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment