Advertisement
Vladi1442

C&O.OOP

Mar 23rd, 2022
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.32 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4.  
  5. // we can stick a function into a class
  6.  
  7. // 3 types: (we can call them modifiers)
  8. // PUBLIC: everyone can see it
  9. // PRIVATE: we can access it just inside the class, it's invisible outside of class
  10. // PROTECTED: just for objects of classes, and their heirs
  11.  
  12. //
  13. class Room1
  14. {
  15. private:
  16.     double length;
  17.     double breadth;
  18.     double height;
  19.  
  20. public:
  21.     void init(double len, double bred, double heig) {
  22.         length = len;
  23.         breadth = bred;
  24.         height = heig;
  25.     }
  26.  
  27.     double calculateArea() {
  28.         return length * breadth;
  29.     }
  30.  
  31.     double calculateVolume() {
  32.         return length * breadth * height;
  33.     }
  34. };
  35.  
  36. //
  37.  
  38. //
  39. class Room
  40. {
  41. public:
  42.     double length;
  43.     double breadth;
  44.     double height;
  45.  
  46.     double calculateArea() {
  47.         return length * breadth;
  48.     }
  49.  
  50.     double calculateVolume() {
  51.         return length * breadth * height;
  52.     }
  53. };
  54.  
  55. //
  56.  
  57. // first class
  58. class YoutubeChannel
  59. {
  60. public:
  61.     //char name[32];
  62.     //char ownerName[24];
  63.     std::string name;
  64.     std::string ownerName;
  65.     int subscriberCount;
  66. };
  67.  
  68. // second class
  69. class Book
  70. {
  71. public:
  72.     std::string author;
  73.     std::string title;
  74.     int pages;
  75. };
  76.  
  77. // third class
  78. class BuckysClass
  79. {
  80. public:
  81.     void coolSaying() {
  82.         std::cout << "Preachin to the choir" << std::endl;
  83.     }
  84. };
  85.  
  86. // forth class
  87. class Geeks
  88. {
  89. public:
  90.     std::string geekName;
  91.     int id;
  92.  
  93.     // it's just declared, it's not defined
  94.     void printName();
  95.  
  96.     // printId is defined into a class
  97.     void printId() {
  98.         std::cout << "Geek id is: " << id;
  99.     }
  100. };
  101.  
  102. // defining a member function
  103. void Geeks::printName() {
  104.     std::cout << "Geek name is: " << geekName << std::endl;
  105. }
  106.  
  107. int main() {
  108.     YoutubeChannel youtubechannel;
  109.     youtubechannel.name = "Vladimir";
  110.     youtubechannel.ownerName = "Bootcamp";
  111.     youtubechannel.subscriberCount = 10000;
  112.  
  113.     std::cout << "Name: " << youtubechannel.name << std::endl;
  114.     std::cout << "Owner name: " << youtubechannel.ownerName << std::endl;
  115.     std::cout << "Subscriber count: " << youtubechannel.subscriberCount << std::endl;
  116.  
  117.     // starting with new object
  118.    
  119.     Book book1;
  120.     book1.author = "Harry Potter";
  121.     book1.title = "JK Rowling";
  122.     book1.pages = 500;
  123.  
  124.     std::cout << book1.author << std::endl;
  125.     std::cout << book1.title << std::endl;
  126.     std::cout << book1.pages << std::endl;
  127.  
  128.     // second object
  129.  
  130.     Book book2;
  131.     book2.author = "Lord of the rings";
  132.     book2.title = "Tolkin";
  133.     book2.pages = 750;
  134.  
  135.     std::cout << book2.author << std::endl;
  136.     std::cout << book2.title << std::endl;
  137.     std::cout << book2.pages << std::endl;
  138.  
  139.     // the youtube example
  140.  
  141.     BuckysClass buckysClass;
  142.     buckysClass.coolSaying();
  143.    
  144.     // geeks for geeks
  145.  
  146.     Geeks geek;
  147.     geek.geekName = "Vladi";
  148.     geek.id = 897;
  149.  
  150.     geek.printName();
  151.     std::cout << std::endl;
  152.  
  153.     geek.printId();
  154.    
  155.     // from the youtube
  156.     //
  157.     // create object of Room class
  158.     Room room1;
  159.  
  160.     // assign values to data members
  161.     room1.length = 42.5;
  162.     room1.breadth = 18.5;
  163.     room1.height = 30.2;
  164.  
  165.     // calculate functions
  166.     std::cout << "Area of room: "  << room1.calculateArea() << std::endl;
  167.     std::cout << "Volume of room: " << room1.calculateVolume() << std::endl;
  168.    
  169.     // similar like previous
  170.  
  171.     Room1 room2;
  172.  
  173.     room2.init(42.5, 30.6, 18.7);
  174.  
  175.     std::cout << "Area of room: " << room2.calculateArea() << std::endl;
  176.     std::cout << "Volume of room: " << room2.calculateVolume() << std::endl;
  177.  
  178.  
  179.     return 0;
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement