Cpp Anatomy

From wikinotes
Revision as of 15:26, 2 July 2022 by Will (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

This section provides generic information to introduce or re-familiarize myself with cpp at a glance.


source/header example

/* ========
 * myfile.h
 * ========
 */
 
// (define functions and classes that can be included in other files) 

#ifndef MYFILE_H
#define MYFILE_H

// functions
void add(int, int);
void sub(int, int);

// methods
class MyClass {
    public:
        void add(int, int);
        void sub(int, int);
}

#endif


/* ==========
 * myfile.cpp
 * ==========
 */

// (actual functions and classes)

#include <iostream>
#include "myfile.h"
using namespace std;


// functions
void add(int a, int b) {
    retval = a + b;
    puts(retval);
}

void sub(int a, int b) {
    int retval;
    retval = a - b;
    puts(retval);
}


// methods
MyClass::add(int a, int b){
    add(a, b);
}
MyClass:sub(int a, int b){
    sub(a, b);
}


// if __name__ == '__main__'
int main() {
    cout << "Hello World\n";                    // prints hello world, and creates a new line
    cout << endl;                               // creates a new line
    cout << "Press Enter to Continue:";
    std::cin.get();                             //Waits for user type something and push enter

	 // do not return from main(), will automatically
	 // return 1 if fail, or 0 if notfail
}


Common Libraries

Most Used Libraries
iostream Instructs program how to deal with input/outputs such as cin and cout. Included in most C++ programs.
math.h
sstream
string
stdlib.h
time.h