Hi there, we’re Harisystems
"Unlock your potential and soar to new heights with our exclusive online courses! Ignite your passion, acquire valuable skills, and embrace limitless possibilities. Don't miss out on our limited-time sale - invest in yourself today and embark on a journey of personal and professional growth. Enroll now and shape your future with knowledge that lasts a lifetime!".
For corporate trainings, projects, and real world experience reach us. We believe that education should be accessible to all, regardless of geographical location or background.
1C++ Basics
C++ is a powerful programming language known for its efficiency, versatility, and performance. Whether you are a beginner or an experienced programmer, understanding the basics of C++ is essential. In this article, we will introduce you to some fundamental concepts of C++ with examples to help you get started.
1. Hello, World!
Let's begin with the classic "Hello, World!" program. This simple program prints the text "Hello, World!" on the console. Here's the code:
#include <iostream>
int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}
Save the above code in a file with the .cpp
extension, such as hello_world.cpp
. Compile and run the program, and you should see the output:
Hello, World!
2. Variables and Data Types
C++ supports various data types for storing different kinds of values. Here are some commonly used data types:
- int: Used for storing whole numbers (integers).
- float: Used for storing floating-point numbers (decimal values).
- char: Used for storing single characters.
- bool: Used for storing boolean values (true or false).
Here's an example that demonstrates the usage of variables and data types:
#include <iostream>
int main() {
int age = 25;
float salary = 2500.50;
char grade = 'A';
bool isEmployed = true;
std::cout << "Age: " << age << std::endl;
std::cout << "Salary: " << salary << std::endl;
std::cout << "Grade: " << grade << std::endl;
std::cout << "Employed: " << isEmployed << std::endl;
return 0;
}
Save the above code in a file, such as variables.cpp
. Compile and run the program to see the output:
Age: 25
Salary: 2500.5
Grade: A
Employed: 1
3. Control Flow
C++ provides various control flow statements to control the execution of your program. Here are a few examples:
- if-else: Used to perform conditional execution based on a condition.
- for loop: Used for executing a block of code repeatedly for a specified number of times.
- while loop: Used for executing a block of code repeatedly as long as a condition is true.
Here's an example that uses a for loop to print numbers from 1 to 10:
#include <iostream>
int main() {
for (int i = 1; i <= 10; i++) {
std::cout << i << " ";
}
std::cout << std::endl;
return 0;
}
Save the above code in a file, such as control_flow.cpp
. Compile and run the program to see the output:
1 2 3 4 5 6 7 8 9 10
4. Functions
Functions allow you to break down your code into smaller, reusable units. Here's an example that defines a function to calculate the factorial of a number:
#include <iostream>
int factorial(int n) {
if (n == 0)
return 1;
else
return n * factorial(n - 1);
}
int main() {
int number = 5;
int result = factorial(number);
std::cout << "Factorial of " << number << " is " << result << std::endl;
return 0;
}
Save the above code in a file, such as functions.cpp
. Compile and run the program to see the output:
Factorial of 5 is 120
These examples provide a glimpse into the basics of C++ programming. As you explore further, you will encounter more concepts, such as arrays, pointers, classes, and object-oriented programming. C++ is a versatile language with extensive capabilities, making it suitable for a wide range of applications.
Remember to practice regularly, experiment with code, and explore additional resources to deepen your understanding of C++ programming.
4.5L
Learners
20+
Instructors
50+
Courses
6.0L
Course enrollments
Future Trending Courses
When selecting, a course, Here are a few areas that are expected to be in demand in the future:.
Future Learning for all
If you’re passionate and ready to dive in, we’d love to join 1:1 classes for you. We’re committed to support our learners and professionals their development and well-being.
View CoursesMost Popular Course topics
These are the most popular course topics among Software Courses for learners