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

Data Types

Uploaded by

mrkwllcfe
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Data Types

Uploaded by

mrkwllcfe
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Data Types

 Fundamental Types: int, char, float, double, bool.

 Derived Types: Arrays, pointers, references.

 User-defined Types: Structs, enums, classes.

Variables

 Declaration: Define the type and name.

 Initialization: Assign a value.

 Scope: Local vs. global.

Operators

 Arithmetic: +, -, *, /, %.

 Relational: ==, !=, <, >, <=, >=.

 Logical: &&, ||, !.

 Bitwise: &, |, ^, ~, <<, >>.

Control Structures

Conditional Statements

 if, else if, else: Decision-making.

 switch: Multiple choice branching.

Loops

 for: Iterates a specific number of times.

 while: Continues until a condition is false.

 do while: Executes at least once.

Break and Continue

 break: Exits a loop.

 continue: Skips the current iteration.

Functions

Function Declaration and Definition

 Return types and parameters.


 Overloading: Same function name, different parameters.

Inline Functions

 Short functions defined within the class.

Recursion

 A function calling itself; involves base and recursive cases.

Object-Oriented Programming

Classes and Objects

 Class Definition: Blueprint for objects.

 Member Variables and Methods: Attributes and behaviors.

Constructors and Destructors

 Constructors: Initialize objects.

 Destructors: Cleanup before object destruction.

Inheritance

 Base and Derived Classes: Reusing code.

 Types: Single and multiple inheritance.

Polymorphism

 Function Overloading: Same name, different implementations.

 Virtual Functions: Allow derived classes to override base class


methods.

Standard Template Library (STL)

Containers

 vector, list, map, set: Data structures.

Algorithms

 Common algorithms: sort, find.

Iterators

 Mechanism to traverse elements in containers.

Memory Management
Dynamic Memory Allocation

 Use new and delete to manage memory manually.

Smart Pointers

 std::unique_ptr, std::shared_ptr, std::weak_ptr: Automatic


memory management.

Exception Handling

 Try, Catch, Throw: Manage errors gracefully.

Additional Tips

 Practice Coding: Use platforms like LeetCode.

 Review Sample Questions: Familiarize with exam formats.

 Understand Key Concepts: Focus on understanding over


memorization.

Resources

 Books:

o "C++ Primer" by Lippman

o "Effective C++" by Scott Meyers

 Online Courses: Coursera, Udemy, edX.

1. Basic Syntax
Hello World Example

cpp

Copy code

#include <iostream>

int main() {

std::cout << "Hello, World!" << std::endl;

return 0;

 Explanation: This program includes the iostream library, defines the


main function, and prints "Hello, World!" to the console.

2. Data Types

Common Data Types

cpp

Copy code

int age = 25; // Integer

double salary = 50000.50; // Floating-point

char initial = 'A'; // Character

bool isEmployed = true; // Boolean

3. Control Structures

Conditional Statements

cpp

Copy code

int number = 10;


if (number > 0) {

std::cout << "Positive\n";

} else if (number < 0) {

std::cout << "Negative\n";

} else {

std::cout << "Zero\n";

Loops

For Loop

cpp

Copy code

for (int i = 0; i < 5; i++) {

std::cout << i << " ";

While Loop

cpp

Copy code

int count = 0;

while (count < 5) {

std::cout << count << " ";

count++;

4. Functions

Function Definition and Call

cpp

Copy code
int add(int a, int b) {

return a + b;

int main() {

int sum = add(5, 10);

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

return 0;

5. Object-Oriented Programming

Classes and Objects

cpp

Copy code

class Dog {

public:

void bark() {

std::cout << "Woof!" << std::endl;

};

int main() {

Dog myDog;

myDog.bark(); // Calls the bark method

return 0;

Constructors and Destructors


cpp

Copy code

class Cat {

public:

Cat() { std::cout << "Cat created!" << std::endl; } // Constructor

~Cat() { std::cout << "Cat destroyed!" << std::endl; } // Destructor

};

int main() {

Cat myCat; // Automatically calls the constructor

return 0; // Destructor is called here

6. Inheritance and Polymorphism

Inheritance Example

cpp

Copy code

class Animal {

public:

void speak() {

std::cout << "Animal speaks" << std::endl;

};

class Cat : public Animal {

public:

void speak() {
std::cout << "Meow" << std::endl; // Overriding

};

int main() {

Cat myCat;

myCat.speak(); // Outputs "Meow"

return 0;

7. Standard Template Library (STL)

Using Vectors

cpp

Copy code

#include <vector>

int main() {

std::vector<int> numbers = {1, 2, 3, 4, 5};

numbers.push_back(6); // Add element

for (int num : numbers) {

std::cout << num << " "; // Output all elements

return 0;

8. Exception Handling

Try-Catch Example
cpp

Copy code

#include <iostream>

int main() {

try {

throw std::runtime_error("An error occurred");

} catch (const std::exception& e) {

std::cout << "Caught: " << e.what() << std::endl;

return 0;

9. Memory Management

Dynamic Memory Allocation

cpp

Copy code

int* ptr = new int; // Allocate memory

*ptr = 5; // Assign value

std::cout << *ptr; // Output value

delete ptr; // Free memory

10. Tips for Success as a Developer

1. Practice Coding: Regularly solve problems on platforms like LeetCode


or HackerRank.

2. Build Projects: Create small projects to apply what you learn.


3. Read Documentation: Familiarize yourself with C++ standards and
libraries.

4. Join Communities: Engage in forums or local developer groups.

Additional Resources

 Books: "C++ Primer" by Lippman, "Effective C++" by Scott Meyers.

 Online Courses: Look for C++ courses on platforms like Coursera or


Udemy.

You might also like