Skip to content

Instantly share code, notes, and snippets.

@sucremad
Last active July 8, 2021 21:14
Show Gist options
  • Save sucremad/866a18789c065aef873366e34bbba5cf to your computer and use it in GitHub Desktop.
Save sucremad/866a18789c065aef873366e34bbba5cf to your computer and use it in GitHub Desktop.
C++ Fundamentals Notes

Fundamentals of C++

#include <iostream>


using namespace std;

// DECLARING FUNCTIONS  

void hello();
void helloyou(string name);
double cube(double num);
string Weekend(int num);




// DECLARING CLASSES

class Book {
    public:
        string title;
        string author;
        int pages;

        // Constructor

        Book() {
            cout << "Creating Book Object..." << endl;
        }
};

class Series {
    public:
        string title;
        string type;
        int season;
        bool cont;

        // Constructor
        // Multiple Constructors are possible.

         Series(string Title, string Type, int Seasons, bool Cont) {
             title = Title;
             type = Type;
             season = Seasons;
             cont = Cont;
         }


         // Object Functions

         bool stillContinue() {
             if (cont)
                 return true;
             else
                 return false;
         }

};

class Painting {

    private: // no access outside this class
        string material;


    public:

        string artist, title, year, movement;

        Painting(string Artist, string Title, string Year, string Movement, string Material) {
            artist = Artist;
            title = Title;
            year = Year;
            movement = Movement;
            setMaterial(Material);
        }

    
        void setMaterial(string sMaterial) {
            if (sMaterial == "oil" || sMaterial == "coal" || sMaterial == "acrylic" || sMaterial == "ink") {
                material = sMaterial;
            }
            else
                material = "Other";
        }
        
        string getMaterial() {
            return material;
        }
            
            

};


// INHERITANCE

class Chef {
    public:

        void makeChicken(){
            cout << "The chef makes chicken." << endl;
        }

        void makeSalad(){
           cout << "The chef makes salad." << endl;
        }

        void makeSpecialDish() {
            cout << "The chef makes bbq ribs." << endl;
        }

};

class TurkishChef : public Chef{
    public:

        void makeSpecialDish() {            // OverWritting is possible
            cout << "The Turkish chef makes Lahmacun." << endl;
        }
        void makeKebab() {               // Adding extra functions is possible
            cout << "The Turkish chef makes Kebab." << endl;
        }

};





int main()
{

    //print to the screen the string and end the line.
    cout << "Hello World!\n" << endl; 


    // Variables: -- containers for data
    // Format: <Type> <name> = <value>;

    string name = "Mlu";  // contains chars
    int age = 16;        // contains number

    // print variables
    cout << "My name is " << name << " and age is " << age << endl;

    // Data Types

    char grade = 'A';                   // holds single character
    string phrase = "Shaking Girl";     // Plain text
    int number = 16;
    double gpa = 3.16;
    bool isSad = false;  // takes two value: true or false


    // STRINGS ---__----------------____--------------------------___---------------

    string lyric = "Big girls cry when their heart is breaking.";

    // print specific part
    cout << lyric[0] << endl; // B

    // Find where is a part starts, start looking from index 0
    cout << lyric.find("cry",0) << endl;

    // take part of a string
    // start from index 25, take 5 chars
    cout << lyric.substr(25, 5) << endl;


    // NUMBERS ---__----------------____--------------------------___---------------
    cout << 12 % 4 << endl;

    int num = 6;
    double dnum = 6.2;

    cout << num + dnum << endl; //12.2
    cout << 10 / 2 << endl;     // 5
    cout << 10 / 2.1 << endl;   // 4.7619

    /*
    power -- power(2, 3)
    square root -- sqrt (5)
     */

    // GET INPUT ---__----------------____--------------------------___---------------


    string animeCharacter;
    cout << "Enter an anime character: ";
    cin >> animeCharacter;  // getline(cin, animeCaharacter)
    cout << "The character is " << animeCharacter << endl;



    // ARRAYS

    int numbers[] = { 1,5,7,8,3 };
    cout << numbers[3];    // 8

    int days[7];  // 7 is the size of the array.



    // FUNCTION CALLS
    // for to call functions, first it needs function declaration above main()

    hello();
    helloyou("Mikasa");
    cube(6.2);



    // CONDITIONAL STATEMENTS

    bool isHappy = false;

    if (!isHappy)
    {
        cout << "go for a walk.";
    }
    else if(isHappy)
    {
        cout << "Stay happy!";
    }
    else {
        cout << "What!";
    }

    //LOOPS

    int index = 1;

    while (index <= 6)
    {
        cout << index << endl;
        index++;
    }

    for (int i = 7; i <= 12; i++)
    {
        cout << i << endl;
    }

  
    // 2D Arrays

    int numberGrid[3][2] = { {1,2}, {3,4}, {5,6} };

    cout << numberGrid[0][1] << endl; //2

    int arrSize = sizeof(numberGrid) / sizeof(numberGrid[0]);


    for (int i = 0; i < arrSize; i++)
    {
        for (int j = 0; j < 2; j++)
        {
            cout << "numberGrid[" << i << "][" << j << "] = " << numberGrid[i][j] << endl;
        }
    }


    // POINTERS
    int luckyNum = 3;
    int* pluckyNum = &luckyNum;

    double dgpa = 3.48;
    string school = "life";
    
    cout << &dgpa << endl;  // memory address of the dgpa
    cout << *&dgpa << endl;  // value of the dgpa

    cout << pluckyNum << endl;          // address of the luckynum
    cout << *pluckyNum << endl;         // value of the luckynum
    cout << &*pluckyNum << endl;        //address of the luckynum
    cout << *&*pluckyNum << endl;       //value of the luckynum


    // CLASSES --------------------------------------------------------------

    Book book1;  // instance of Book class
    book1.author = "Sadegh Hedayat";
    book1.title = "The Blind Owl";
    book1.pages = 95;

    Book book2;  
    book2.author = "Stefan Zweig";
    book2.title = "Fear";
    book2.pages = 64;

    cout << book1.title << endl;
    cout << book2.pages << endl;

    Series serie1("Hunter X Hunter", "Anime", 6, true);

    // Getters and Setters -------------------------------------------------

    Painting paint1("Vincent Van Gogh", "Bulb Fields", "1512", "Realism", "oil");
    cout << paint1.getMaterial() << endl;  // oil
    

    Painting paint2("Albrecht Dürer", "Wing of a European Roller", "1883", "Realism", "ink");
    cout << paint2.getMaterial() << endl;  // ink
    paint2.setMaterial("watercolor");
    cout << paint2.getMaterial() << endl;  // other

    // INHERITANCE -----------------------------------------------------------

    Chef chef1;
    chef1.makeSalad();   
    chef1.makeSpecialDish();

    TurkishChef Tchef;
    Tchef.makeSalad();
    Tchef.makeSpecialDish();
    Tchef.makeKebab();

} // end of Main()


// FUNCTION DEFINITIONS

void hello() {                                  // wout parameter
    cout << "Hello!" << endl;
}

void helloyou(string name) {                    // with parameters
    cout << "Hello! " << name << endl;

}

double cube(double num) {
    double result = pow(num, 3);
    return result;
}

// Switch - Case

string Weekend(int num) {

    switch (num)
    {
    case 0:
        return "Saturday";
        break;
    case 1:
        return "Monday";
        break;
    default:
        return "Enter only 0 or 1";
        break;
    }
    return "Somethings wrong :/";

}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment