Skip to content

Instantly share code, notes, and snippets.

@oshyam
Created May 20, 2021 11:12
Show Gist options
  • Save oshyam/9a6598186d2d1d00d6a9018f3eabfca6 to your computer and use it in GitHub Desktop.
Save oshyam/9a6598186d2d1d00d6a9018f3eabfca6 to your computer and use it in GitHub Desktop.
C++ cheat sheet programs for Beginner and Competitive Exams
// A Simple hello world
/*
Note:C++ is case-sensitive. So, expect compilation to fail if you write
Int instead of int and Std::Cout instead of std::cout.
*/
#include<iostream>
int main(){
std::cout<<"Hello World!"<<std::endl;
}
// Understanding Namespace
// Preprocessor directive
#include <iostream>
// Start of your program
int main()
{
// Tell the compiler what namespace to search in
using namespace std;
/*
Write to the screen using std::cout
*/
cout << "Hello World" << endl;
// Return a value to the OS
return 0;
}
//Declaring, Defining, and Calling a Function That Demonstrates Capabilities
of std::cout
#include <iostream>
using namespace std;
// Declare a function
int DemoConsoleOutput();
int main()
{
// Call i.e. invoke the function
DemoConsoleOutput();
return 0;
}
// Define i.e. implement the previously declared function
int DemoConsoleOutput()
{
cout << "This is a simple string literal" << endl;
cout << "Writing number five: " << 5 << endl;
cout << "Performing division 10 / 5 = " << 10 / 5 << endl;
cout << "Pi when approximated is 22 / 7 = " << 22 / 7 << endl;
cout << "Pi is 22 / 7 = " << 22.0 / 7 << endl;
return 0;
}
//C++ Flow Control
/*
There are three forms of if...else statements in C++.
if statement
if...else statement
if...else if...else statement
*/
/*
if (condition) {
// body of if statement
}
if...else statement
if (condition) {
// block of code if condition is true
}
else {
// block of code if condition is false
}
-- if...else if...else statement
if (condition1) {
// code block 1
}
else if (condition2){
// code block 2
}
else {
// code block 3
}
*/
#include<iostream>
using namespace std;
int main(){
int num;
cout<<"Input a Number"<<endl;
cin>>num;
if(num>=0){
cout<<"Positive Number"<<endl;
}else
{
cout<<"Negative Number"<<endl;
}
}
switch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}
int day = 4;
switch (day) {
case 1:
cout << "Monday";
break;
case 2:
cout << "Tuesday";
break;
case 3:
cout << "Wednesday";
break;
case 4:
cout << "Thursday";
break;
case 5:
cout << "Friday";
break;
case 6:
cout << "Saturday";
break;
case 7:
cout << "Sunday";
break;
}
// Outputs "Thursday" (day 4)
//Function in c plus
/*
There are two types of function:
1.Standard Library Functions: Predefined in C++
2. User-defined Function: Created by users
Below is the example of user defined function
*/
#include <iostream>
using namespace std;
// declaring a function
void heyReader() {
cout << "Hello there! Thank You for reading this section";
}
int main() {
// calling the function
heyReader();
return 0;
}
//In computer programming, loops are used to repeat a block of code.
/*
There are 3 types of loops in C++.
for loop
while loop
do...while loop
*/
//FOR LOOP
for (initialization; condition; update) {
// body of-loop
}
//WHILE LOOP
while (condition) {
// body of the loop
}
//DO-WHILE LOOP
do {
// body of loop;
}
while (condition);
//infinite loop example
#include <iostream>
using namespace std;
int main() {
// Write C++ code here
for(int i=1;i>0;i++){
cout<<"hacked!"<<endl;
}
return 0;
}
//In C++, the size and type of arrays cannot be changed after its declaration.
/*
syntax :
dataType arrayName[arraySize];
declare and initialize and array
int x[6] = {19, 10, 8, 17, 9, 15};
// declare and initialize an array
int x[] = {19, 10, 8, 17, 9, 15};
*/
//EXAMPLE - Take Inputs from User and Store Them in an Array
#include <iostream>
using namespace std;
int main() {
int numbers[5];
cout << "Enter 5 numbers: " << endl;
// store input from user to array
for (int i = 0; i < 5; ++i) {
cin >> numbers[i];
}
cout << "The numbers are: ";
// print array elements
for (int n = 0; n < 5; ++n) {
cout << numbers[n] << " ";
}
return 0;
}
//POINTER
/*
Pointers are used for file handling. Pointers are used to allocate memory dynamically.
In C++, a pointer declared to a base class could access the object of a derived class.
However, a pointer to a derived class cannot access the object of a base class.
*/
#include <iostream>
using namespace std;
int main() {
int age=21;
int* agePointer;
agePointer=&age;
cout<<agePointer;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment