Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- // we can stick a function into a class
- // 3 types: (we can call them modifiers)
- // PUBLIC: everyone can see it
- // PRIVATE: we can access it just inside the class, it's invisible outside of class
- // PROTECTED: just for objects of classes, and their heirs
- //
- class Room1
- {
- private:
- double length;
- double breadth;
- double height;
- public:
- void init(double len, double bred, double heig) {
- length = len;
- breadth = bred;
- height = heig;
- }
- double calculateArea() {
- return length * breadth;
- }
- double calculateVolume() {
- return length * breadth * height;
- }
- };
- //
- //
- class Room
- {
- public:
- double length;
- double breadth;
- double height;
- double calculateArea() {
- return length * breadth;
- }
- double calculateVolume() {
- return length * breadth * height;
- }
- };
- //
- // first class
- class YoutubeChannel
- {
- public:
- //char name[32];
- //char ownerName[24];
- std::string name;
- std::string ownerName;
- int subscriberCount;
- };
- // second class
- class Book
- {
- public:
- std::string author;
- std::string title;
- int pages;
- };
- // third class
- class BuckysClass
- {
- public:
- void coolSaying() {
- std::cout << "Preachin to the choir" << std::endl;
- }
- };
- // forth class
- class Geeks
- {
- public:
- std::string geekName;
- int id;
- // it's just declared, it's not defined
- void printName();
- // printId is defined into a class
- void printId() {
- std::cout << "Geek id is: " << id;
- }
- };
- // defining a member function
- void Geeks::printName() {
- std::cout << "Geek name is: " << geekName << std::endl;
- }
- int main() {
- YoutubeChannel youtubechannel;
- youtubechannel.name = "Vladimir";
- youtubechannel.ownerName = "Bootcamp";
- youtubechannel.subscriberCount = 10000;
- std::cout << "Name: " << youtubechannel.name << std::endl;
- std::cout << "Owner name: " << youtubechannel.ownerName << std::endl;
- std::cout << "Subscriber count: " << youtubechannel.subscriberCount << std::endl;
- // starting with new object
- Book book1;
- book1.author = "Harry Potter";
- book1.title = "JK Rowling";
- book1.pages = 500;
- std::cout << book1.author << std::endl;
- std::cout << book1.title << std::endl;
- std::cout << book1.pages << std::endl;
- // second object
- Book book2;
- book2.author = "Lord of the rings";
- book2.title = "Tolkin";
- book2.pages = 750;
- std::cout << book2.author << std::endl;
- std::cout << book2.title << std::endl;
- std::cout << book2.pages << std::endl;
- // the youtube example
- BuckysClass buckysClass;
- buckysClass.coolSaying();
- // geeks for geeks
- Geeks geek;
- geek.geekName = "Vladi";
- geek.id = 897;
- geek.printName();
- std::cout << std::endl;
- geek.printId();
- // from the youtube
- //
- // create object of Room class
- Room room1;
- // assign values to data members
- room1.length = 42.5;
- room1.breadth = 18.5;
- room1.height = 30.2;
- // calculate functions
- std::cout << "Area of room: " << room1.calculateArea() << std::endl;
- std::cout << "Volume of room: " << room1.calculateVolume() << std::endl;
- // similar like previous
- Room1 room2;
- room2.init(42.5, 30.6, 18.7);
- std::cout << "Area of room: " << room2.calculateArea() << std::endl;
- std::cout << "Volume of room: " << room2.calculateVolume() << std::endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement