0% found this document useful (0 votes)
16 views

C++ Notes

Notes from a video

Uploaded by

hua.flower.cai
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

C++ Notes

Notes from a video

Uploaded by

hua.flower.cai
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 87

Learning

Video: https://www.youtube.com/watch?v=vLnPwxZdW4Y

Int main() is a function and it gets executed every time we run the
program.

Drawing triangle manually:


#include <iostream>

using namespace std;

int main()
{
cout << " /|" << endl;
cout << " / |" << endl;
cout << " / |" << endl;
cout << "/___|" << endl;

return 0;
}
Variable type Examples Format

string Text string greeting = “Hello”;


string greeting;
greeting = “Hello”;

int Numbers int num = 5;

int num;
num = 5;

Printing a sentence with a variable:


#include <iostream>

using namespace std;

int main()
{
string name = "Audric";
int age = 14;

cout << name << " is " << age << " years old." << endl;

return 0;
}
Data type C++ word Format

character char char character = ‘A’;


string string string animal = “Giraffe”;

integers int int x = 5;

decimals float float y = 2.3;

decimals double float z = 2.44;

true or false bool bool answer = true;


float and double stores decimals. The only difference is that double stores
twice as many decimal numbers as a float.

#include <iostream>

using namespace std;

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";

cout << "Today is ";


cout << today;

return 0;
}
\n can also be used to go to the line below

#include <iostream>

using namespace std;

int main()
{
string today = "Tuesday";

cout << "Today is:\n";


cout << today;

return 0;
}

.length() can be used to find the length of the string.

#include <iostream>
using namespace std;

int main()
{
string phrase = "This is a phrase.";

cout << phrase.length();

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.";

cout << phrase[0];

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';

cout << phrase;

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>

using namespace std;

int main()
{
string phrase = "This is a phrase.";

cout << phrase.find("This",0);

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>

using namespace std;

int main()
{
string phrase = "This is a phrase.";

cout << phrase.substr(2, 5);

return 0;
}

Operator Symbol

Addition +

Subtraction -
Multiplication *

Division /

Remainder %

Increment 1 ++

Increment -1 --

Add a number to the variable +=

Subtract a number from the variable -=

Multiply a number with the variable *=

Divide a number from the variable /=

10 / 3 will result in 3 since 10 / 3 are integers. To result in 3.333…. It must


be formatted like 10.0 / 3.0.

To tell the C++ compiler that we want to use some functions, we must type
#include <cmath>

Function What it does Parameter 1 Parameter 2

pow() Raises the base Base number Exponent of


number to the power
exponent of
power

Example:
#include <iostream>
#include <cmath>

using namespace std;


int main()
{

cout << pow(2,5);

return 0;
}
sqrt() Square roots the The number to N/A
number be square
rooted

Example:
#include <iostream>
#include <cmath>

using namespace std;

int main()
{

cout << sqrt(50);

return 0;
}
round() Round the digit The number to N/A
to the nearest be rounded.
whole number

Example:
#include <iostream>
#include <cmath>

using namespace std;

int main()
{

cout << round(5.3124);

return 0;
}
ceil() Round up the The number to N/A
number be rounded up

floor() Round down the The number to N/A


number be rounded
down

Example:
#include <iostream>
#include <cmath>

using namespace std;

int main()
{

cout << ceil(4.1) << endl;

cout << floor(4.9);

return 0;
}

fmax() Find the bigger First number Second number


number out of
the two

fmin() Find the smaller First number Second number


number out of
the two

Example:
#include <iostream>
#include <cmath>

using namespace std;

int main()
{

cout << fmax(1,10) << endl;

cout << fmin(1,10);

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>

using namespace std;

int main()
{
int age;
cout << "Input your age: ";
cin >> age;

cout << "Your age is " << age;


return 0;
}

For multiple texts of input, another term is used: getline(cin, var name).
Example:
#include <iostream>

using namespace std;

int main()
{
string name;
cout << "Input your name: ";
getline(cin, name);

cout << "Your name is " << name;

return 0;
}
Adding two digits:
#include <iostream>

using namespace std;


int main()
{
int num1, num2;
cout << "Input your first number: ";
cin >> num1;

cout << "Input your second number: ";


cin >> num2;

cout << "Your sum is " << num1 + num2;

return 0;
}
Mad libs game
#include <iostream>

using namespace std;


int main()
{
string Name, Food, TV;

cout << "Input your name: ";


getline(cin, Name);
cout << "Input your favourite food: ";
getline(cin, Food);
cout << "Input a TV show: ";
getline(cin, TV);

cout << "Hello, my name is " << Name << endl;


cout << Food << " is my favourite food" << endl;
cout << "I like watching " << TV << endl;

return 0;
}
Array format: variable_type variable_name[] = {}
#include <iostream>

using namespace std;


int main()
{
char Name[] = {'A','B','C'};

cout << Name[0];

return 0;
}
To change an index value in an array, type array_name[index_number] = …
#include <iostream>

using namespace std;


int main()
{
char Name[] = {'A','B','C'};

Name[0] = 'Z';

cout << Name[0];

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';

cout << Name[0];

return 0;
}
Functions:
int main(){

}
The int main function is a function that is played all the time

To make another function, type void function_name(){

}
Note that this function has to be defined before intmain(){}
Example:
#include <iostream>

using namespace std;

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;

void Hello(string Name){


cout << "Hello " << Name;
}

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>

using namespace std;

double Cube(double n){


double result = n * n * n;
return result;
}

int main()
{
double ans = Cube(5.0);
cout << ans;

return 0;
}
It can also be formatted like this:
#include <iostream>

using namespace std;


double Cube(double n){
return n * n * n;
}

int main()
{
cout << Cube(5.0);

return 0;
}
if statements are formatted as the following:
if(condition){}
Example:
#include <iostream>

using namespace std;

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>

using namespace std;

int main()
{
bool isMale = true;
bool isTall = false;

if(isMale && isTall){


cout << "You are a tall male";
}else if(isMale && !isTall){
cout << "You are a short male";
}else{
cout << "You are neither a male or a tall human";
}

return 0;
}
Finding the bigger number out of 2 numbers using if statements:
#include <iostream>

using namespace std;


int big(int A, int B){
int result;
if(A > B){
result = A;
} else if(A < B) {
result = B;
} else{
result = A;
}
return result;
}

int main()
{
int num1, num2;
cout << "Input a number: ";
cin >> num1;
cout << "Input another number: ";
cin >> num2;

cout << "The bigger number is " << big(num1, 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;

cout << "" << endl;

cout << "Input your first number: ";


cin >> n1;
cout << "" << endl;

cout << "Input your second number: ";


cin >> n2;
cout << "" << endl;

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>

using namespace std;

string daysoftheweek(int num){


string day;

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: ";

cin >> num;

cout << daysoftheweek(num);


}
While loop format: while(condition)({}

#include <iostream>
using namespace std;

int main()
{
int x = 1;

while(x < 5){


cout << x << endl;
x++;
}
}
There is another format for while loops: do{}while(condition);
The difference is that the second format runs the program first before
checking the condition so even if the condition is false, it will still run the
loop once.
#include <iostream>

using namespace std;

int main()
{
int x = 1;

do{
cout << x << endl;
x++;
}while(x < 1);
}
Guessing game:
#include <iostream>

using namespace std;


int main()
{
int secretNum = 7;
int guess;
int guessCount = 0;
int guessLimit = 3;
bool outOfGuesses = false;

while(secretNum != guess && !outOfGuesses){


if(guessCount < guessLimit){
cout << "Enter guess: ";
cin >> guess;
guessCount++;
}else{
outOfGuesses = true;
}
}

if(outOfGuesses){
cout << "You lost!";
}else{
cout << "You won!";
}
return 0;
}
For loop format: for(decalare variable; condition; increment;){}
#include <iostream>

using namespace std;


int main()
{
int x[] = {1, 2, 3, 4, 5};

for(int i = 0; i <= 4; i++){


cout << x[i] << ",";
}
return 0;
}
Making a function for exponents:
#include <iostream>

using namespace std;


int exp(int base, int power){
int result = 1;
for(int i = 0; i < power; i++){
result = result * base;
}

return result;
}

int main()
{
int x, y;
cin >> x;
cin >> y;

cout << exp(x, y);

return 0;
}
2d arrays: Inside every element of an array, there is another array.

Format: variable_type variable_name[][] = {{,},{,}}


#include <iostream>
using namespace std;

int main()
{
int coordinates[3][2] = {
{1,2},
{3,4},
{5,6}
};

for(int i = 0; i <= 2; i++){


for(int j = 0; j <= 1; j++){
cout << coordinates[i][j];
}
cout << endl;
}
}
Comments are formatted as: //. Comments do not run at all
#include <iostream>

using namespace std;


int main()
{
//int coordinates[3][2] = {
// {1,2},
// {3,4},
// {5,6}
//};

//for(int i = 0; i <= 2; i++){


// for(int j = 0; j <= 1; j++){
// cout << coordinates[i][j];
// }
// cout << endl;
//}
}
To print the address of the variable, add & before printing the variable.
Pointers are variables that stores the memory address of an object.
To store the memory address, the following format is used: variable_type
*pVariable_Name2 = &Variable_Name1
#include <iostream>

using namespace std;

int main()
{
int x = 10;
int *pX = &x;

cout << "The address of variable x is " << pX << endl;

cout << "The variable x is " << *pX;


}
Classes are basically creating new data types. Format for class: class
class_name{parameters};
#include <iostream>
using namespace std;

class people{
public:
string name;
int age;
string gender;
};

int main()
{
people grade;
grade.name = "Ryan";
grade.age = 15;
grade.gender = "Male";

cout << grade.name << grade.age << grade.gender;


}
A constructor in C++ is a special function that is automatically called when
an object of a class is created. Constructor functions format are as follows:
class_name(){}
#include <iostream>
using namespace std;

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;

students(string aname, int aage, string agender){


name = aname;
age = aage;
setgender(agender);
}

void setgender(string agender){


if(gender == "Male" || gender == "Female"){
gender = agender;
}else{
gender = "Invalid";
}
}

string getgender(){
return gender;
}
};

int main(){
students A("A", 5, "Male");
A.setgender("Dog");

cout << A.getgender();


}
Inheritance: Creating classes inside a class and having all those classes
inherit the original functionality. Format:
class 2nd_class : public 1st_class{};
#include <iostream>

using namespace std;

class choice{
public:
void A(){
cout << "First" << endl;
}
void B(){
cout << "Second" << endl;
}
void C(){
cout << "Third" << endl;
}
};

class choice2 : public choice{


};

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

%ld Long int

%lld Long long 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;

scanf("%d %ld %c %f %lf", &i, &lo, &ch, &fl, &d);

printf("%d\n%ld\n%c\n%.3f\n%.9lf", i, lo, ch, fl, d);


return 0;
}
Practice questions
Binary search:

Question website: https://www.hackerrank.com/domains/cpp


Date Challenges solved

16 Feb 2023 Say “Hello, World!” With C++


Input and Output
Basic Data Types

17 Feb 2023 For Loop


Functions

19 Feb 2023 Pointer


Array Introduction
(learned an array of vectors)
Variable sized arrays
Conditional statements
(finished introduction)

You might also like