Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //tutorial 9
- #include <iostream>
- #include <string>
- #include <iomanip>
- using namespace std;
- class IGeometricCalc{
- public:
- virtual void SetGeometricAttributes(float side1, float side2, float radius) = 0;
- virtual float CalculateSurfaceArea() = 0;
- virtual float CalculateVolume() = 0;
- };
- class CubeGeometricCalc : public IGeometricCalc{
- private:
- float side;
- float area;
- float volume;
- public:
- virtual void SetGeometricAttributes(float side1, float side2, float radius)
- {
- side = side1;
- }
- virtual float CalculateSurfaceArea() {
- area = 6 * pow(side, 2);
- return area;
- }
- virtual float CalculateVolume() {
- volume = side*side*side;
- return volume;
- }
- };
- class SphereGeometricCalc: public IGeometricCalc{
- private:
- float area;
- float volume;
- float rad;
- public:
- virtual void SetGeometricAttributes(float side1, float side2, float radius){
- rad = radius;
- }
- virtual float CalculateSurfaceArea() {
- area = 4 * 3.1415*rad *rad;
- return area;
- }
- virtual float CalculateVolume() {
- volume = (4.0 / 3.0)*3.1415 *rad*rad*rad;
- return volume;
- }
- };
- class CylinderGeometricCalc : public IGeometricCalc{
- private:
- float area;
- float volume;
- float rad;
- float h;
- public:
- virtual void SetGeometricAttributes(float side1, float side2, float radius){
- rad = radius;
- h = side1;
- }
- virtual float CalculateSurfaceArea() {
- area = (2 * 3.1415*rad *h)+ (2 * 3.1415*rad *rad);
- return area;
- }
- virtual float CalculateVolume() {
- volume = 3.1415 *rad*rad*h;
- return volume;
- }
- };
- int main(){
- float areacube;
- float volumecube;
- float areasphere;
- float volumesphere;
- float areacyl;
- float volumecyl;
- IGeometricCalc *pcube = new CubeGeometricCalc();
- pcube->SetGeometricAttributes(3, 4, 5);
- areacube=pcube->CalculateSurfaceArea();
- cout << "area cube :" << areacube << endl;
- volumecube=pcube->CalculateVolume();
- cout << "volume cube :" << volumecube << endl;
- IGeometricCalc *psphere = new SphereGeometricCalc();
- psphere->SetGeometricAttributes(3, 4, 5);
- areasphere = psphere->CalculateSurfaceArea();
- cout << "area sphere :" << areasphere << endl;
- volumesphere = psphere->CalculateVolume();
- cout << "volume sphere :" << volumesphere << endl;
- IGeometricCalc *pcyl = new CylinderGeometricCalc();
- pcyl->SetGeometricAttributes(3, 4, 5);
- areacyl = pcyl->CalculateSurfaceArea();
- cout << "area cyl :" << areacyl << endl;
- volumecyl = pcyl->CalculateVolume();
- cout << "volume cyl :" << volumecyl << endl;
- delete pcube;
- delete pcyl;
- delete psphere;
- return 0;
- }
- #include <iostream>
- #include <string>
- #include <iomanip>
- using namespace std;
- class IStudent{
- public:
- virtual void SetName(string &studentName) = 0;
- virtual void SetId(unsigned long studentId)=0;
- virtual void SetMajor(string &studentMajor) = 0;
- virtual void PrintDetails() =0;
- };
- class Undergraduate :public IStudent{
- private:
- string name;
- string major;
- unsigned long id;
- public:
- virtual void SetName(string &studentName) {
- name = studentName;
- }
- virtual void SetId(unsigned long studentId) {
- id = studentId;
- }
- virtual void SetMajor(string &studentMajor) {
- major = studentMajor;
- }
- virtual void PrintDetails(){
- cout << "name" << name << endl;
- cout << "major" << major << endl;
- cout << "id" << id << endl;
- }
- };
- int main(){
- string name = "liyana";
- string major = "tele";
- IStudent *pstudent = new Undergraduate();
- pstudent->SetName(name);
- pstudent->SetMajor(major);
- pstudent->SetId(1112702565);
- pstudent->PrintDetails();
- delete pstudent;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement