C++ Notes
C++ Notes
Video: https://www.youtube.com/watch?v=vLnPwxZdW4Y
Int main() is a function and it gets executed every time we run the
program.
int main()
{
cout << " /|" << endl;
cout << " / |" << endl;
cout << " / |" << endl;
cout << "/___|" << endl;
return 0;
}
Variable type Examples Format
int num;
num = 5;
int main()
{
string name = "Audric";
int age = 14;
cout << name << " is " << age << " years old." << endl;
return 0;
}
Data type C++ word Format
#include <iostream>
int main()
{
char character = 'a';
string animal = "Giraffe";
int x = 5;
float y = 2.3;
double z = 2.44;
bool answer = false;
cout << character << animal << x << y << z << answer << endl;
return 0;
}
endl is similar to the enter key. It goes to the next line so the next printed
sentence is on the line below.
#include <iostream>
using namespace std;
int main()
{
string today = "Tuesday";
return 0;
}
\n can also be used to go to the line below
#include <iostream>
int main()
{
string today = "Tuesday";
return 0;
}
#include <iostream>
using namespace std;
int main()
{
string phrase = "This is a phrase.";
return 0;
}
To print out only a certain letter [] can be used at the end of the variable
with the index in the brackets.
#include <iostream>
using namespace std;
int main()
{
string phrase = "This is a phrase.";
return 0;
}
[] can also be used to change a specific letter of the phrase.
#include <iostream>
using namespace std;
int main()
{
string phrase = "This is a phrase.";
phrase[0] = 'H';
return 0;
}
.find() can be used to find a certain phrase or character or number in a
variable. In between the brackets, there are parameters or arguments we
need to add. In .find(“This”), “This” tells the function to find “This” in the
variable. Other than the word or number, another parameter has to be
made. After “This”, there has to be a comma and a number. For example
.find(“This”, 0). 0 will tell the function to return the index number of “This”
in the variable.
#include <iostream>
int main()
{
string phrase = "This is a phrase.";
return 0;
}
.substr() is a function that allows us to take a part of the variable. It has
two parameters. The first piece of information is the starting index. The
second parameter inputted is the number of characters.
#include <iostream>
int main()
{
string phrase = "This is a phrase.";
return 0;
}
Operator Symbol
Addition +
Subtraction -
Multiplication *
Division /
Remainder %
Increment 1 ++
Increment -1 --
To tell the C++ compiler that we want to use some functions, we must type
#include <cmath>
Example:
#include <iostream>
#include <cmath>
return 0;
}
sqrt() Square roots the The number to N/A
number be square
rooted
Example:
#include <iostream>
#include <cmath>
int main()
{
return 0;
}
round() Round the digit The number to N/A
to the nearest be rounded.
whole number
Example:
#include <iostream>
#include <cmath>
int main()
{
return 0;
}
ceil() Round up the The number to N/A
number be rounded up
Example:
#include <iostream>
#include <cmath>
int main()
{
return 0;
}
Example:
#include <iostream>
#include <cmath>
int main()
{
return 0;
}
Inputting:
To ask a question, set the variable first. Then, print a text telling the user to
input a message. The term for input is: cin >>. This only applies for
numbers or one characters.
Example:
#include <iostream>
int main()
{
int age;
cout << "Input your age: ";
cin >> age;
For multiple texts of input, another term is used: getline(cin, var name).
Example:
#include <iostream>
int main()
{
string name;
cout << "Input your name: ";
getline(cin, name);
return 0;
}
Adding two digits:
#include <iostream>
return 0;
}
Mad libs game
#include <iostream>
return 0;
}
Array format: variable_type variable_name[] = {}
#include <iostream>
return 0;
}
To change an index value in an array, type array_name[index_number] = …
#include <iostream>
Name[0] = 'Z';
return 0;
}
When declaring an array, the number in the square brackets: variable_type
variable_name[number] = {} is the number of maximum elements in the
array.
#include <iostream>
using namespace std;
int main()
{
char Name[20];
Name[0] = 'Z';
return 0;
}
Functions:
int main(){
}
The int main function is a function that is played all the time
}
Note that this function has to be defined before intmain(){}
Example:
#include <iostream>
void Hello(){
cout << "Hello";
}
int main()
{
Hello();
return 0;
}
The brackets of the function can have a parameter which is a variable
name. This variable can be inputted and used in the function.
Example:
#include <iostream>
using namespace std;
int main()
{
string name;
cout << "Input your name: ";
getline(cin, name);
Hello(name);
return 0;
}
“Return” basically returns the result of the function. It can be used in void
functions and int main(){}. Note that “void” can be replaced by any data
type.
Example:
#include <iostream>
int main()
{
double ans = Cube(5.0);
cout << ans;
return 0;
}
It can also be formatted like this:
#include <iostream>
int main()
{
cout << Cube(5.0);
return 0;
}
if statements are formatted as the following:
if(condition){}
Example:
#include <iostream>
int main()
{
bool isMale = true;
if(isMale){
cout << "You are a male";
}
return 0;
}
Else and else if is formatted as
if(){
}else if(){
}else {
}
Symbol Meaning
&& AND
|| OR
! Not
Example:
#include <iostream>
int main()
{
bool isMale = true;
bool isTall = false;
return 0;
}
Finding the bigger number out of 2 numbers using if statements:
#include <iostream>
int main()
{
int num1, num2;
cout << "Input a number: ";
cin >> num1;
cout << "Input another number: ";
cin >> num2;
return 0;
}
Better calculator:
#include <iostream>
#include <cmath>
using namespace std;
int main(){
double n1, n2;
int op;
cout << "Which of the following operations would you like to do?" <<
endl;
cout << "1 = +" << endl;
cout << "2 = -" << endl;
cout << "3 = *" << endl;
cout << "4 = /" << endl;
cout << "5 = ^" << endl;
cin >> op;
if(op == 1){
cout << "The sum is " << n1 + n2;
}else if(op == 2){
cout << "The difference is " << n1 - n2;
}else if(op == 3){
cout << "The product is " << n1 * n2;
}else if(op == 4){
cout << "The quotient is " << n1 / n2;
}else if(op == 5){
cout << "The exponent is " << pow(n1, n2);
}else{
cout << "Your input is invalid, try again.";
}
return 0;
}
Switch, case, break, and default:
A switch allows a variable to be compared to all the digits in the switch.
The digits are all in “cases” so the variable is compared to the digits in the
cases. Break statements essentially stop the switch statement from
looking for more digits. Default is the program that the switch goes to if
none of the other digits are correct.
Example:
#include <iostream>
switch(num){
case 1:
day = "Monday";
break;
case 2:
day = "Tuesday";
break;
case 3:
day = "Wednesday";
break;
case 4:
day = "Thursday";
break;
case 5:
day = "Friday";
break;
case 6:
day = "Saturday";
break;
case 7:
day = "Sunday";
break;
default:
day = "Invalid day";
break;
}
return day;
}
int main()
{
int num;
cout << "Input the day of the week you want to print: ";
#include <iostream>
using namespace std;
int main()
{
int x = 1;
int main()
{
int x = 1;
do{
cout << x << endl;
x++;
}while(x < 1);
}
Guessing game:
#include <iostream>
if(outOfGuesses){
cout << "You lost!";
}else{
cout << "You won!";
}
return 0;
}
For loop format: for(decalare variable; condition; increment;){}
#include <iostream>
return result;
}
int main()
{
int x, y;
cin >> x;
cin >> y;
return 0;
}
2d arrays: Inside every element of an array, there is another array.
int main()
{
int coordinates[3][2] = {
{1,2},
{3,4},
{5,6}
};
int main()
{
int x = 10;
int *pX = &x;
class people{
public:
string name;
int age;
string gender;
};
int main()
{
people grade;
grade.name = "Ryan";
grade.age = 15;
grade.gender = "Male";
class people{
public:
string name;
int age;
string gender;
people(string aname, int aage, string agender){
name = aname;
age = aage;
gender = agender;
}
};
int main()
{
people apeople("Ryan", 15, "Male");
cout << apeople.name << ", " << apeople.age << ", " << apeople.gender;
}
Getters and setters: control access to the different attributes and different
elements inside the C++ class. First, set it as “private”. Private means that
only the code inside the class can be access the variable.
#include <iostream>
using namespace std;
class students{
private:
string gender;
public:
string name;
int age;
string getgender(){
return gender;
}
};
int main(){
students A("A", 5, "Male");
A.setgender("Dog");
class choice{
public:
void A(){
cout << "First" << endl;
}
void B(){
cout << "Second" << endl;
}
void C(){
cout << "Third" << endl;
}
};
int main(){
choice A;
A.A();
choice2 B;
B.B();
}
When inputting and outputting so many object, scanf and printf is much
faster.
Format Accepts
%d int
%f float
%lf double
%c char
%s string
Format:
#include <cstdio>
scanf(format, &var names, … );
Example:
#include <iostream>
#include <cstdio>
using namespace std;
int main() {
int i;
long lo;
char ch;
float fl;
double d;