0% found this document useful (0 votes)
9 views

Introduction To C++

Uploaded by

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

Introduction To C++

Uploaded by

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

1.

Introduction to C++:
C++ is an extension of the C language, developed by Bjarne Stroustrup in 1983.
It is a general-purpose, object-oriented programming (OOP) language.
C++ supports both procedural and object-oriented programming paradigms, making it
more flexible than C.
It is widely used in system/software development, game development, and competitive
programming.
2. Basic Syntax:
Every C++ program starts with the main() function, like in C.
cpp
Copy code
#include <iostream>
using namespace std;

int main() {
cout << "Hello, World!";
return 0;
}
Header Files: #include <iostream> for input-output operations.
Namespace: using namespace std; is used to avoid writing std:: repeatedly.
Semicolons ; are used to terminate statements.
3. Data Types:
Primitive Types: int, float, double, char, bool, etc.
cpp
Copy code
int age = 22;
char grade = 'A';
bool isValid = true;
Derived Types: Arrays, pointers, references, classes, structures, and unions.
Void: Special type representing "no type," often used for functions.
4. Input/Output:
cin and cout: Used for input and output in C++.
cpp
Copy code
int age;
cout << "Enter your age: ";
cin >> age;
cout << "Your age is: " << age;
5. Operators:
Arithmetic Operators: +, -, *, /, %.
Relational Operators: ==, !=, <, >, <=, >=.
Logical Operators: && (AND), || (OR), ! (NOT).
Increment/Decrement Operators: ++, --.
6. Control Structures:
If-Else Statements:
cpp
Copy code
if (age > 18) {
cout << "Adult";
} else {
cout << "Minor";
}
Switch Statements:
cpp
Copy code
switch (age) {
case 18:
cout << "You are 18!";
break;
default:
cout << "Not 18!";
}
Loops:
For Loop:
cpp
Copy code
for (int i = 0; i < 5; i++) {
cout << i << endl;
}
While Loop:
cpp
Copy code
int i = 0;
while (i < 5) {
cout << i << endl;
i++;
}
Do-While Loop:
cpp
Copy code
int i = 0;
do {
cout << i << endl;
i++;
} while (i < 5);
7. Functions:
Functions in C++ are declared using the return type, function name, and parameters.
cpp
Copy code
int add(int a, int b) {
return a + b;
}

int main() {
int sum = add(5, 3);
cout << "Sum: " << sum;
return 0;
}
C++ supports function overloading, allowing functions to have the same name but
different parameters.
8. Object-Oriented Programming (OOP):
Classes and Objects: A class is a blueprint for creating objects.
cpp
Copy code
class Person {
public:
string name;
int age;

void introduce() {
cout << "Name: " << name << ", Age: " << age;
}
};

int main() {
Person p1;
p1.name = "Sumit";
p1.age = 22;
p1.introduce();
return 0;
}
Encapsulation: Grouping data (variables) and methods (functions) in a class.
Inheritance: Deriving a class from another class.
cpp
Copy code
class Student : public Person {
public:
string school;
};
Polymorphism: Ability to take many forms, like function overloading or overriding.
Abstraction: Hiding complex details and showing only essential information.
9. Constructors and Destructors:
Constructor: A special function automatically called when an object is created.
cpp
Copy code
class Person {
public:
Person() {
cout << "Constructor Called!";
}
};
Destructor: Automatically called when an object is destroyed.
cpp
Copy code
~Person() {
cout << "Destructor Called!";
}
10. Pointers and References:
Pointers: Store the memory address of a variable.
cpp
Copy code
int x = 10;
int *ptr = &x;
cout << *ptr; // Dereferencing
References: Another name for an existing variable.
cpp
Copy code
int a = 10;
int &ref = a;
ref++;
cout << a; // Output: 11
11. Memory Management:
Dynamic Memory Allocation: Using new and delete.
cpp
Copy code
int *ptr = new int;
*ptr = 5;
delete ptr;
Smart Pointers: Introduced in C++11, they manage memory automatically (e.g.,
shared_ptr, unique_ptr).

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy