0% found this document useful (0 votes)
24 views14 pages

Declaration of Variable

The document discusses C++ variable declarations, data types, operators, functions, function overloading, inline functions, and default function parameters. It includes code examples demonstrating how to declare variables of basic data types, use operators, define and call functions, overload functions, and use default function parameters.

Uploaded by

sotaroedits
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views14 pages

Declaration of Variable

The document discusses C++ variable declarations, data types, operators, functions, function overloading, inline functions, and default function parameters. It includes code examples demonstrating how to declare variables of basic data types, use operators, define and call functions, overload functions, and use default function parameters.

Uploaded by

sotaroedits
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Declaration of Variable:-

#include <iostream>
using namespace std;

int main() {
int num = 10; // Declaration and initialization of a single variable
cout << "The value of num is: " << num << endl;
return 0;
}

#include <iostream>
using namespace std;

int main() {
int num1, num2, sum;
float floatNum;

num1 = 5;
num2 = 7;
sum = num1 + num2;
floatNum = 3.14f;

cout << "num1 = " << num1 << endl;


cout << "num2 = " << num2 << endl;
cout << "sum = " << sum << endl;
cout << "floatNum = " << floatNum << endl;

return 0;
}
Float
#include <iostream>
using namespace std;

int main() {
// Declaration of float variable
float floatValue = 3.14f;

// Output the float value


cout << "The value of floatValue is: " << floatValue << endl;

return 0;
}

Double

#include <iostream>
using namespace std;

int main() {
// Declaration of double variable
double doubleValue = 3.14159;

// Output the double value


cout << "The value of doubleValue is: " << doubleValue << endl;

return 0;
}
OPERATORS:-

#include <iostream>
using namespace std;

int main() {
int num1 = 10, num2 = 5;
int sum = num1 + num2;
int difference = num1 - num2;
int product = num1 * num2;
int quotient = num1 / num2;

cout << "Sum: " << sum << endl;


cout << "Difference: " << difference << endl;
cout << "Product: " << product << endl;
cout << "Quotient: " << quotient << endl;

return 0;
}

#include <iostream>
using namespace std;

int main() {
int num1 = 10, num2 = 5;
cout << "num1 > num2: " << (num1 > num2) << endl;
cout << "num1 < num2: " << (num1 < num2) << endl;
cout << "num1 == num2: " << (num1 == num2) << endl;
cout << "num1 != num2: " << (num1 != num2) << endl;

return 0;
}
#include <iostream>
using namespace std;

int main() {
bool a = true, b = false;
cout << "a && b: " << (a && b) << endl;
cout << "a || b: " << (a || b) << endl;
cout << "!a: " << !a << endl;
cout << "!b: " << !b << endl;

return 0;
}

#include <iostream>
using namespace std;

int main() {
int num = 5;
num += 3;
cout << "Value of num after += operation: " << num << endl;
num -= 2;
cout << "Value of num after -= operation: " << num << endl;
num *= 4;
cout << "Value of num after *= operation: " << num << endl;
num /= 2;
cout << "Value of num after /= operation: " << num << endl;

return 0;
}
Scope Resolution:-

#include <iostream>
using namespace std;

int globalVar = 100; // Global variable

void displayGlobal() {
// Accessing global variable using scope resolution operator
cout << "Inside function: globalVar = " << ::globalVar << endl;
}

int main() {
// Accessing global variable from main function
cout << "Inside main: globalVar = " << globalVar << endl;

// Calling function to display global variable


displayGlobal();

return 0;
}
#include <iostream>
using namespace std;

class MyClass {
public:
static int myStaticVar; // Static member variable

void displayStaticVar() {
// Accessing static member variable using scope resolution operator
cout << "Inside MyClass::displayStaticVar(): myStaticVar = " << myStaticVar << endl;
}
};

int MyClass::myStaticVar = 50; // Definition of static member variable

int main() {
// Accessing static member variable from outside the class
cout << "Outside MyClass: myStaticVar = " << MyClass::myStaticVar << endl;

// Creating object of MyClass and calling member function


MyClass obj;
obj.displayStaticVar();

return 0;
}
#include <iostream>
using namespace std;
#define nline "\n"

string name1 = "Eleanor";


string favFood = "Sushi";
string universityName = "ABC University";

class Student {
public:
string name = "Charlie";
string favoriteSubject = "Math";
string university = "XYZ College";

Student(string name, string favSubject, string university)


: name(name)
, favoriteSubject(favSubject)
, university(university)
{
}
};

int main() {
Student obj = Student("Daniel", "Computer Science", "DEF Institute");
cout << "Name: " << obj.name << endl;
cout << "Favorite Subject: " << obj.favoriteSubject << endl;
cout << "University: " << obj.university << nline;

cout << "Global Variables:" << nline;


cout << "Global Name: " << name1 << endl;
cout << "Global Favorite Food: " << favFood << endl;
cout << "Global University: " << universityName << nline;

return 0;
}
#include <iostream>
using namespace std;

int localVar = 200; // Global variable

int main() {
int localVar = 300; // Local variable inside main function

cout << "The value of the local variable localVar: " << localVar << endl;
cout << "The value of the global variable localVar: " << ::localVar << endl;

return 0;
}

#include <iostream>
using namespace std;

class MyClass {
public:
void displayMessage(); // Declaration of member function
};

// Definition of member function outside the class


void MyClass::displayMessage() {
cout << "This is a member function of the class." << endl;
}

int main() {
MyClass obj; // Creating an object of the class
obj.displayMessage(); // Calling the member function

return 0;
}
In Line Function:-

#include <iostream>
using namespace std;

// Inline function definition


inline int add(int a, int b) {
return a + b;
}

int main() {
int num1 = 5, num2 = 7;
int result = add(num1, num2); // Function call is replaced with actual function body
cout << "Result: " << result << endl;
return 0;
}

#include <iostream>
using namespace std;

// Inline void function definition


inline void displayMessage() {
cout << "This is an inline void function." << endl;
}

int main() {
displayMessage(); // Function call is replaced with actual function body
return 0;
}
#include <iostream>
using namespace std;

// Inline addition function


inline int add(int a, int b) {
return a + b;
}

int main() {
int num1 = 5, num2 = 7;
int result = add(num1, num2);
cout << "Result of addition: " << result << endl;
return 0;
}

#include <iostream>
using namespace std;

// Inline max function


inline int max(int a, int b) {
return (a > b) ? a : b;
}

int main() {
int num1 = 5, num2 = 7;
int maximum = max(num1, num2);
cout << "Maximum of " << num1 << " and " << num2 << " is: " << maximum << endl;
return 0;
}
Function Calling:-

#include <iostream>
using namespace std;

int add(int a, int b) { return a + b; }

int main() {
int num1, num2;
cout << "Enter two numbers: ";
cin >> num1 >> num2;
cout << "Sum: " << add(num1, num2) << endl;
return 0;
}

#include <iostream>
using namespace std;

int factorial(int n) { return (n <= 1) ? 1 : n * factorial(n - 1); }

int main() {
int num;
cout << "Enter a number: ";
cin >> num;
cout << "Factorial: " << factorial(num) << endl;
return 0;
}
#include <iostream>
using namespace std;

// Function to find the maximum of two numbers


int max(int a, int b) {
return (a > b) ? a : b;
}

int main() {
int num1, num2;
cout << "Enter two numbers: ";
cin >> num1 >> num2;
cout << "Maximum: " << max(num1, num2) << endl;
return 0;
}

Function Overloading:-

#include <iostream>
using namespace std;

// Function overloading to add two integers


int add(int a, int b) { return a + b; }

// Function overloading to add two floats


float add(float a, float b) { return a + b; }

int main() {
cout << "Sum of integers: " << add(5, 7) << endl;
cout << "Sum of floats: " << add(3.5f, 2.5f) << endl;
return 0;
}
#include <iostream>

int add(int x, int y) {


return x + y;
}

double add(double x, double y) {


return x + y;
}

int main() {
std::cout << "Sum of 10 and 20: " << add(10, 20) << std::endl;
std::cout << "Sum of 3.7 and 4.5: " << add(3.7, 4.5) << std::endl;
return 0;
}

#include <iostream>

void print(int num1, int num2 = 0, int num3 = 0) {


int sum = num1 + num2 + num3;
std::cout << "Sum of numbers: " << sum << std::endl;
}

int main() {
print(5);
print(2, 4);
print(1, 2, 4);
return 0;
}

You might also like