Programming C++

Input/Output

Output
------
printf:

printf("My name is %s\n","Methsaan");
printf("My age is %d\n",99);
printf("My weight is %f Kg\n",18.44443);
printf("Long long int %lld\n", 223692584);
printf("Long int %ld\n", 1251);

cout:

cout << 24 << endl;
cout << "My name is " << "Methsaan" << endl;
cout << "this is a float " << 4.3545453 << endl;

puts:

puts("test 123");
No formating allowed.
Only strings.
Puts a new line.

putchar:

putchar('M')
Always single quote.
No formatting allowed.
No new line.
Only single characters.

Input
-----
scanf:

scanf("%dcm", &heigth); //expecting the user to write a number followed by cm, example: 15cm
scanf("%f%d", &weigth, &day); //expects user to enter float, space, or new line followed by integer

cin:

getchar:

gets:

fgets: (user enters more than the length of the array, extra input is ignored)

Variables

'if' statements

'for' statements

'while' statements

'do while' statements

'switch' statements

Functions

Classes - defining a re-usable object

What is a class ? A definition of an object. An object can be a collection of functions,
variables and other objects. Can be a new variable type - made up of standard variable
types.

Define a class: private property(variable) and public method(function)

class car {
    public:
        void drive() {
            cout << "Driving" << endl;
        }
        void setcolor(string col){
            color = col;
        }
        string getcolor(){
            return color;
        }
    private:
        string color;
};

int main(int argc, char* argv[])
{
    ....
}

Creating an object from the above class 'definition':

int main(int argc, char* argv[]) {
    car carobject;
    carobject.setcolor("orange");
    carobject.drive();
}

Define a class 2: private property(variable) and public method(function) and private method(function)

~~
#include 
using namespace std;

class train {
    public:
        void goForward(){
            cout << "This is the public go_forward function" << endl;
            if( speed > 100 ) {
                 stopNow();
            }
        }
        int getSpeed() {
            return speed;
        }
        void setSpeed(int mainSpeed) {
            speed = mainSpeed;
        }
    private:
        void stopNow() {
            cout << "Stopping the train as it has reached the " << speed<< endl;
        }
        int speed;
};

int main(int argc, char *argv[]){
    train trainObject;
    trainObject.setSpeed(50);
    trainObject.goForward();
}
~~~

Define a class 3: printing the speed and input

~~~
//get user input for speed
#include 
using namespace std;

class train {
    public:
        void goForward(int speed_){
            cout << "This is the public go_forward function" << endl;
            cout << "Going forward at speed " << speed_ << endl;
            if( speed > speedLimit ) {
                 stopNow();
            }
        }
        int getSpeed() {
            return speed;
        }
        void setSpeed(int mainSpeed) {
            speed = mainSpeed;
        }

    private:
        int speedLimit = 100;
        int speed;
        void stopNow() {
            cout << "Stopping the train as it has reached the " << speed<< endl;
        }
};

class getUserInput {
    public:
        int getSpeed() {
            cout << "Enter forward speed: ";
            cin >> speed;
            return speed;
        }
        int _speed_() {
            return speed;
        }
    private:
        int speed;
};
int main(int argc, char *argv[]){
    train trainObject;
    getUserInput speedInput;
    trainObject.setSpeed(speedInput.getSpeed());
    trainObject.goForward(speedInput._speed_());
}
~~~

Private, public, protected members:
Public members are accessible in main, other classes and inherited classes.
Private members are only accessible inside the class.
Protected members are only accessible inside the class and in inherited classes.

Inheritance - one class inherits attributes - members (functions and data variables) from other class objects

Polymorphism

Constructors and de-constructors

Pointers - access variables by their memory location

Arrays

Structures - collection of data variables

Pre-processors

Function templates

Exceptions

Other languages

Programming Main Page