C PROGRAMMING
Basic Syntax and Structure
Description: The fundamental structure of a C program includes functions, variables, and statements.
Example:
#include <stdio.h>
int main()
{
printf("Hello, World!");
return 0;
}
Data Types and Variables
Description: C provides basic data types like int, float, char, etc., to store different types of data.
Example:
int age = 25;
float salary = 50000.50;
char grade = 'A';
Operators
Description: C includes operators for arithmetic, logical, relational, etc.
Example:
int a = 5, b = 3;
int sum = a + b; // Arithmetic Operator
int isEqual = (a == b); // Relational Operator
Control Statements
Description: Control the flow of execution using if, else, switch, for, while, etc.
Example:
int num = 10;
if (num > 5)
{
printf("Number is greater than 5");
}
Loops
Description: for, while, and do-while loops help in repeating tasks.
Example:
for (int i = 1; i <= 5; i++)
{
printf("%d ", i); // Prints 1 2 3 4 5
}
Functions
Description: Functions allow code to be organized and reused.
Example:
int add(int x, int y)
{
return x + y;
}
int main()
{
int result = add(3, 4);
printf("Sum: %d", result);
return 0;
}
Arrays
Description: Arrays store multiple values of the same type in a single variable.
Example:
int numbers[3] = {1, 2, 3};
printf("%d", numbers[0]); // Outputs 1
Pointers
Description: Pointers store memory addresses and allow for memory manipulation.
Example:
int num = 10;
int *ptr = #
printf("%d", *ptr); // Dereferences ptr to print 10
Strings
Description: C strings are arrays of characters ending with a null character \0.
Example:
char name[] = "Alice";
printf("%s", name);
Structures
Description: Structures group variables of different types.
Example:
struct Person {
char name[50];
int age;
};
struct Person person = {"your name",19};
File I/O
Description: Allows reading from and writing to files.
Example:
FILE *file = fopen("data.txt", "w");
fprintf(file, "Hello, File!");
fclose(file);
C ++ PROGRAMMING
Basic Syntax and Structure
Description: C++ syntax includes functions, variables, and statements.
Example:
#include <iostream>
using namespace std;
int main() {
cout << "Hello, World!";
return 0;
}
Data Types and Variables
Description: C++ has data types like int, float, char, and allows auto for type inference.
Example:
int age = 25;
float salary = 50000.50;
char grade = 'A';
auto isStudent = true;
Operators
Description: Includes arithmetic, logical, relational, etc., operators.
Example:
int a = 5, b = 3;
int sum = a + b; // Arithmetic Operator
bool isEqual = (a == b); // Relational Operator
Control Statements
Description: Control the flow with if, else, switch, for, while, etc.
Example:
int num = 10;
if (num > 5) {
cout << "Number is greater than 5";
}
Loops
Description: for, while, and do-while loops for repeating tasks.
Example:
for (int i = 1; i <= 5; i++) {
cout << i << " "; // Outputs: 1 2 3 4 5
}
Functions
Description: Functions for organized, reusable code.
Example:
int add(int x, int y) {
return x + y;
}
int main() {
int result = add(3, 4);
cout << "Sum: " << result;
return 0;
}
Classes and Objects (Object-Oriented Programming)
Description: Classes define objects with attributes and behaviors.
Example:
class Car {
public:
string model;
void start() {
cout << model << " started.";
}
};
int main() {
Car car1;
car1.model = "Toyota";
car1.start(); // Outputs: Toyota started.
return 0;
}
Inheritance
Description: Allows a class to inherit from another.
Example:
class Vehicle {
public:
void move() { cout << "Moving"; }
};
class Car : public Vehicle {};
int main() {
Car myCar;
myCar.move(); // Outputs: Moving
return 0;
}
Polymorphism
Description: Functions or methods with the same name but different behavior.
Example:
class Animal {
public:
virtual void sound() { cout << "Animal sound"; }
};
class Dog : public Animal {
public:
void sound() override { cout << "Bark"; }
};
int main() {
Animal *a = new Dog();
a->sound(); // Outputs: Bark
return 0;
}
Templates
Description: Templates enable generic programming for functions and classes.
Example:
template <typename T>
T add(T x, T y) {
return x + y;
}
int main() {
cout << add<int>(3, 4); // Outputs: 7
cout << add<double>(3.5, 4.5); // Outputs: 8.0
return 0;
}
Exception Handling
Description: Manages errors with try, catch, and throw.
Example:
try {
int divisor = 0;
if (divisor == 0) throw "Division by zero!";
} catch (const char* msg) {
cout << msg;
}
Standard Template Library (STL)
Description: Provides built-in classes and functions, such as vectors, sets, maps, etc.
Example:
#include <vector>
#include <iostream>
using namespace std;
int main() {
vector<int> nums = {1, 2, 3};
nums.push_back(4);
for (int n : nums) {
cout << n << " "; // Outputs: 1 2 3 4
}
return 0;
}