C++ in one post

Below you you find the existence of what you need for a program in C++. This is written as a reference but could also be seen as tutorial.

/*
* Multi-line comment
*/

Includes

#include <iostream>// Tells the compiler iostream library which contains the function cout
#include // Allows us to use vectors
#include // Allows us to use strings
#include // Allow us to work with files

Namespace


using namespace std;// Allows functions in the std namespace to be used without their prefix
// std::cout becomes cout

———- FUNCTIONS ———-

The function has return type, function name and attributes with their data types. The attribute data types must match the value passed in. This data is passed by value. You can define default values to attributes as long as they come last.This is known as a function prototype


int addNumbers(int firstNum, int secondNum = 0){
int combinedValue = firstNum + secondNum;
return combinedValue;
}

An overloaded function has the same name, but different attributes


int addNumbers(int firstNum, int secondNum, int thirdNum){
return firstNum + secondNum + thirdNum;
}

A recursive function is one that calls itself


int getFactorial(int number){
int sum;
if(number == 1) sum = 1;
else sum = (getFactorial(number - 1) * number);
return sum;

// getFactorial(2) [Returns 2] * 3
// getFactorial(1) [Returns 1] * 2 <This value goes above>
// 2 * 3 = 6

}

Doesn’t have a return type so use void. Since I’m getting a pointer use int*. Refer to the referenced variable with *age


void makeMeYoung(int* age){
cout << "I used to be " << *age << endl;
*age = 21;
}

A function that receives a reference can manipulate the value globally

void actYourAge(int& age){
age = 39;
}

———- CLASSES ———-

Classes user defined data structures (methods).
A class can have private, public, protected. Within each class there can be methods that have veriables that can also be public static or private.

private

A method or variable that can only be seen and used within a class

class Animal
{
private:
int height;
int weight;
string name;

static

A variable shares the same value with every object in the class

static int numOfAnimals;
protected

A method available to members of the same class and sub classes. Static methods aren’t attached to an object and can only access static member variables

static int getNumOfAnimals() { return numOfAnimals; }

public

A method or variable that can be accessed by anything with access to the object


public:
int getHeight(){return height;}
int getWeight(){return weight;}
string getName(){return name;}
void setHeight(int cm){ height = cm; }
void setWeight(int kg){ weight = kg; }
void setName(string dogName){ name = dogName; }

constructors

A constructor is used in a class to defined how a method within the class can be called

prototype
void setAll(int, int, string);
constructor
Animal(int, int, string);
deconstructor
~Animal();
overloaded constructor
Animal();

Take note that the following method will be overwritten in Dog

void toString();
};
int Animal::numOfAnimals = 0;
protoype

The following method setAll


void Animal::setAll(int height, int weight, string name){

// This is used to refer to an object created of this class type
this -> height = height;
this -> weight = weight;
this -> name = name;
Animal::numOfAnimals++;

}

A constructor is called when an object is created


Animal::Animal(int height, int weight, string name) {

this -> height = height;
this -> weight = weight;
this -> name = name;

}

In C++ a destructor is very importatn to avoid all sorts of data leaks, security issues and memory allocation.
The following destructor is called when an object is destroyed.

Animal::~Animal() {

cout << "Animal " << this -> name << " destroyed" << endl;

}

// A constructor called when no attributes are passed
Animal::Animal() {
numOfAnimals++;
}

// This method prints object info to screen and will be overwritten
void Animal::toString(){
cout << this -> name << " is " << this -> height << " cms tall and "
<< this -> weight << " kgs in weight" << endl;

}

// We can inherit the variables and methods of other classes
class Dog : public Animal{

private:
string sound = "Woof";
public:
void getSound() { cout << sound << endl; }

// Declare the constructor
Dog(int, int, string, string);

// Declare the default constructor and call the default superclass
// constructor
Dog() : Animal(){};

// Overwrite toString
void toString();

};

// Dog constructor passes the right attributes to the superclass
// constructor and then handles the attribute bark that remains
Dog::Dog(int height, int weight, string name, string bark) :
Animal(height, weight, name){

this -> sound = bark;

}

// toString method overwritten
void Dog::toString(){

// Because the attributes were private in Animal they must be retrieved
// by called the get methods
cout << this -> getName() << " is " << this -> getHeight() <<
" cms tall and " << this -> getWeight() << " kgs in weight and says " <<
this -> sound << endl;

}

———- VARIABLES / DATA TYPES ———-

// Variables start with a letter and can contain letters, numbers and _
// They are case sensitive

// A value that won’t change is a constant
// Starts with const and it should be uppercase

const double PI = 3.1415926535;

// chars can contain 1 character that are surrounded with ‘ and is one byte in size

char myGrade = 'A';

// bools have the value of (true/1) or (false/0)

bool isHappy = true;

// ints are whole numbers

int myAge = 39;

// floats are floating point numbers accurate to about 6 decimals

float favNum = 3.141592;

// doubles are floating point numbers accurate to about 15 digits

double otherFavNum = 1.6180339887;

// You can output a variable value like this

cout << "Favorite Number " << favNum << endl;

Leave a Reply

Your email address will not be published. Required fields are marked *