Advertisement
TheWhiteFang

tutorial 9

Jan 13th, 2015
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.64 KB | None | 0 0
  1. //tutorial 9
  2.  
  3. #include <iostream>
  4. #include <string>
  5. #include <iomanip>
  6. using namespace std;
  7.  
  8. class IGeometricCalc{
  9. public:
  10.     virtual void SetGeometricAttributes(float side1, float side2, float radius) = 0;
  11.     virtual float CalculateSurfaceArea() = 0;
  12.     virtual float CalculateVolume() = 0;
  13.  
  14. };
  15.  
  16. class CubeGeometricCalc : public IGeometricCalc{
  17.  
  18. private:
  19.     float side;
  20.     float area;
  21.     float volume;
  22.  
  23. public:
  24.     virtual void SetGeometricAttributes(float side1, float side2, float radius)
  25.     {
  26.         side = side1;
  27.     }
  28.  
  29.     virtual float CalculateSurfaceArea() {
  30.  
  31.         area = 6 * pow(side, 2);
  32.  
  33.         return area;
  34.  
  35.     }
  36.  
  37.     virtual float CalculateVolume() {
  38.         volume = side*side*side;
  39.         return volume;
  40.     }
  41.  
  42.    
  43. };
  44. class SphereGeometricCalc: public IGeometricCalc{
  45.  
  46. private:
  47.     float area;
  48.     float volume;
  49.     float rad;
  50.  
  51. public:
  52.     virtual void SetGeometricAttributes(float side1, float side2, float radius){
  53.  
  54.         rad = radius;
  55.     }
  56.  
  57.     virtual float CalculateSurfaceArea() {
  58.  
  59.         area = 4 * 3.1415*rad *rad;
  60.  
  61.         return area;
  62.  
  63.     }
  64.     virtual float CalculateVolume() {
  65.         volume = (4.0 / 3.0)*3.1415 *rad*rad*rad;
  66.         return volume;
  67.     }
  68.  
  69. };
  70. class CylinderGeometricCalc : public IGeometricCalc{
  71.  
  72.  
  73. private:
  74.     float area;
  75.     float volume;
  76.     float rad;
  77.     float h;
  78.  
  79. public:
  80.     virtual void SetGeometricAttributes(float side1, float side2, float radius){
  81.  
  82.         rad = radius;
  83.         h = side1;
  84.     }
  85.  
  86.     virtual float CalculateSurfaceArea() {
  87.  
  88.         area = (2 * 3.1415*rad *h)+ (2 * 3.1415*rad *rad);
  89.  
  90.         return area;
  91.  
  92.     }
  93.     virtual float CalculateVolume() {
  94.         volume = 3.1415 *rad*rad*h;
  95.         return volume;
  96.     }
  97.  
  98.  
  99. };
  100.  
  101. int main(){
  102.     float areacube;
  103.     float volumecube;
  104.     float areasphere;
  105.     float volumesphere;
  106.     float areacyl;
  107.     float volumecyl;
  108.  
  109.  
  110.  
  111.     IGeometricCalc *pcube = new CubeGeometricCalc();
  112.     pcube->SetGeometricAttributes(3, 4, 5);
  113.     areacube=pcube->CalculateSurfaceArea();
  114.     cout << "area cube  :" << areacube << endl;
  115.     volumecube=pcube->CalculateVolume();
  116.     cout << "volume cube  :" << volumecube << endl;
  117.  
  118.  
  119.     IGeometricCalc *psphere = new SphereGeometricCalc();
  120.     psphere->SetGeometricAttributes(3, 4, 5);
  121.     areasphere = psphere->CalculateSurfaceArea();
  122.     cout << "area sphere  :" << areasphere << endl;
  123.     volumesphere = psphere->CalculateVolume();
  124.     cout << "volume sphere  :" << volumesphere << endl;
  125.  
  126.  
  127.  
  128.     IGeometricCalc *pcyl = new CylinderGeometricCalc();
  129.     pcyl->SetGeometricAttributes(3, 4, 5);
  130.     areacyl = pcyl->CalculateSurfaceArea();
  131.     cout << "area cyl  :" << areacyl << endl;
  132.     volumecyl = pcyl->CalculateVolume();
  133.     cout << "volume cyl  :" << volumecyl << endl;
  134.  
  135.    
  136.  
  137.     delete pcube;
  138.     delete pcyl;
  139.     delete psphere;
  140.     return 0;
  141. }
  142.  
  143.  
  144. #include <iostream>
  145. #include <string>
  146. #include <iomanip>
  147. using namespace std;
  148.  
  149. class IStudent{
  150.  
  151. public:
  152.     virtual void SetName(string &studentName) = 0;
  153.     virtual void SetId(unsigned long studentId)=0;
  154.     virtual void SetMajor(string &studentMajor) = 0;
  155.     virtual void PrintDetails() =0;
  156.  
  157. };
  158. class Undergraduate :public IStudent{
  159. private:
  160.  
  161.     string name;
  162.     string major;
  163.     unsigned long id;
  164.  
  165.  
  166. public:
  167.     virtual void SetName(string &studentName) {
  168.  
  169.         name = studentName;
  170.     }
  171.     virtual void SetId(unsigned long studentId) {
  172.         id = studentId;
  173.  
  174.     }
  175.     virtual void SetMajor(string &studentMajor) {
  176.         major = studentMajor;
  177.    
  178.     }
  179.     virtual void PrintDetails(){
  180.  
  181.         cout << "name" << name << endl;
  182.         cout << "major" << major << endl;
  183.         cout << "id" << id << endl;
  184.     }
  185.  
  186. };
  187.  
  188.  
  189. int main(){
  190.  
  191.     string name = "liyana";
  192.     string major = "tele";
  193.     IStudent *pstudent = new Undergraduate();
  194.     pstudent->SetName(name);
  195.     pstudent->SetMajor(major);
  196.     pstudent->SetId(1112702565);
  197.     pstudent->PrintDetails();
  198.     delete pstudent;
  199.     return 0;
  200.  
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement