Access and Modify Attribute of Class
We are doing access attributes of class and then modify it.
#include <iostream> using namespace std; class MyClass { // The class public: // Public access specifier int x; // Public attribute (int variable) }; int main() { MyClass myObj; // Create an object of MyClass // Access attributes and set values myObj.x = 15; // Print values cout << myObj.x; return 0; }
Call an Function
We are doing call an function with multiple times.
#include <iostream> using namespace std; void myFunction() { cout << "I just got executed!\n"; } int main() { myFunction(); myFunction(); myFunction(); return 0; }
Comparision of Two Numbers
We are doing compare two integers numbers and print result of its.
#include <iostream> using namespace std; int main() { cout << (10 == 15); return 0; }
Create an Object of Class
We are doing create an object of class then called constructor automatically
#include <iostream> using namespace std; class MyClass { // The class public: // Access specifier MyClass() { // Constructor cout << "Hello World!"; } }; int main() { MyClass myObj; // Create an object of MyClass (this will call the constructor) return 0; }
Print Sum Of Two Numbers
we are doing print sum of two numbers with the help of addition operator.
#include <iostream> using namespace std; int main() { int x = 100 + 50; cout << x; return 0; }
Print Sum Of Variable and Number
we are doing print sum of variable and numbers with the help of addition operator.
#include <iostream> using namespace std; int main() { int sum1 = 100 + 50; // 150 (100 + 50) int sum2 = sum1 + 250; // 400 (150 + 250) int sum3 = sum2 + sum2; // 800 (400 + 400) cout << sum1 << "\n"; cout << sum2 << "\n"; cout << sum3; return 0; }