Basic syntax and functions from the C++ programming language.
#include <iostream>
using namespace std;
int main() {
cout << " Meezan ";
return 0;
}
It prints output on the screen
cout << "This is C++ Programming";
It takes input from the user
cin >> variable_name
The data type is the type of data
Typically a single octet(one byte). It is an integer type
char variable_name;
The most natural size of integer for the machine
int variable_name;
A single-precision floating-point value
A double-precision floating-point value
Represents the absence of the type
float variable_name;
double variable_name;
void
bool
A comment is a code that is not executed by the compiler, and the programmer uses it to keep track of the code.
// It's a single line comment
/* It's a
multi-line
comment
*/
It is a collection of characters surrounded by double quotes
// Include the string library
#include <string>
// String variable
string variable1 = "Hello World";
It is used to concatenate two strings
string firstName = "Haris";
string lastName = "malik";
string fullName = firstName.append(lastName);
cout << fullName;
It returns the length of the string
#include<iostream>
using namespace std;
int main() {
cout << "Welcome To Alfurqaan";
return 0;
}
string variable1 = "Hello World";
variable1[1] = 'i';
cout << variable1;
C++ provides some built-in math functions that help the programmer to perform mathematical operations efficiently.
It returns the larger value among the two
It returns the smaller value among the two
It returns the square root of a supplied number
cout << max(25, 140);
cout << min(55, 50);
//sqrt function
#include <iostream>
using namespace std;
int main() {
cout << "Welcome To Alfurqaan";
return 0;
}
if (condition) {
// This block of code will get executed, if the condition is True
}
if (condition) {
// If condition is True then this block will get executed
} else {
// If condition is False then this block will get executed
}
if (condition) {
// Statements;
}
else if (condition){
// Statements;
}
else{
// Statements
}
It is shorthand of an if-else statement.
variable = (condition) ? expressionTrue : expressionFalse;
It allows a variable to be tested for equality against a list of values (cases).
switch (expression)
{
case constant-expression:
statement1;
statement2;
break;
case constant-expression:
statement;
break;
...
default:
statement;
}
while (/* condition */)
{
/* code block to be executed */
}
do
{
/* code */
} while (/* condition */);
for (int i = 0; i < count; i++)
{
/* code */
}
break keyword inside the loop is used to terminate the loop
continue keyword skips the rest of the current iteration of the
loop and returns to the starting point of the loop
break;
continue;
Pointer is a variable that holds the memory address of another variable
datatype *var_name;
var_name = &variable2;
It is a programming approach that primarily focuses on using objects and classes. The objects can be any real-world entities.
class Class_name {
public: // Access specifier
// fields
// functions
// blocks
};
Class_name ObjectName;
It is a special method that is called automatically as soon as the object is created.
class className { // The class
public: // Access specifier
className() { // Constructor
cout << "Haris malik";
}
};
int main() {
className obj_name;
return 0;
}
Data encapsulation is a mechanism of bundling the data, and the functions that use them and data abstraction is a mechanism of exposing only the interfaces and hiding the implementation details from the user.
#include <iostream>
using namespace std;
class ExampleEncap{
private:
/* Since we have marked these data members private,
* any entity outside this class cannot access these
* data members directly, they have to use getter and
* setter functions.
*/
int num;
char ch;
public:
/* Getter functions to get the value of data members.
* Since these functions are public, they can be accessed
* outside the class, thus provide the access to data members
* through them
*/
int getNum() const {
return num;
}
char getCh() const {
return ch;
}
/* Setter functions, they are called for assigning the values
* to the private data members.
*/
void setNum(int num) {
this->num = num;
}
void setCh(char ch) {
this->ch = ch;
}
};
int main(){
ExampleEncap obj;
obj.setNum(100);
obj.setCh('A');
cout<<obj.getNum()<<endl;
cout<<obj.getCh()<<endl;
return 0;
}
#include<iostream>
#include <iostream>
using namespace std;
int main() {
// Create and open a text file
ofstream MyFile("filename.txt");
// Write to the file
MyFile << "File Handling in C++";
// Close the file
MyFile.close();
}
Opens the file to read(default for ifstream)
fs.open ("test.txt", std::fstream::in)
Opens the file to write(default for ofstream)
fs.open ("test.txt", std::fstream::out)