labs 1-6: init

Signed-off-by: Amneesh Singh <natto@weirdnatto.in>
This commit is contained in:
2022-12-24 14:03:08 +05:30
commit ad185c994e
26 changed files with 2250 additions and 0 deletions

73
lab6/17.cpp Normal file
View File

@@ -0,0 +1,73 @@
#include <iostream>
#include <math.h>
using namespace std;
class Shape {
int stub;
public:
virtual void getData() { cout << "stub"; }
virtual double area() {
cout << "stub";
return 0;
}
};
class Triangle : public Shape {
private:
double sides[3];
public:
void getData();
double area();
};
void Triangle::getData() {
cout << "Enter 3 integers for the sides of triangle: \n";
for (int i = 0; i < 3; i++)
cin >> sides[i];
}
double Triangle::area() {
double s = (sides[0] + sides[1] + sides[2]) / 2;
return sqrt(s * (s - sides[0]) * (s - sides[1]) * (s - sides[2]));
}
class Rectangle : public Shape {
private:
double l;
double b;
public:
void getData();
double area();
};
void Rectangle::getData() {
cout << "Enter length of rectangle: ";
cin >> l;
cout << "Enter breadh of rectangle: ";
cin >> b;
}
double Rectangle::area() { return l * b; }
int main() {
Shape *s;
Triangle t;
Rectangle r;
s = &t;
s->getData();
cout << "Area of the Shape: " << s->area() << endl;
s = &r;
s->getData();
cout << "Area of the Shape: " << s->area() << endl;
}