Unit-1 - C++ Programming Basic
Unit-1 - C++ Programming Basic
1. Class
2. Object
An object is an instance of a class. It is a real entity that occupies memory and can perform
actions defined by its class. For instance, if Student is a class, then s1 and s2 are objects of that
class, each with their own set of data.
3. Abstraction
Abstraction means showing only the essential features of an object and hiding
unnecessary details. In C++, abstraction is achieved using classes and access specifiers (public,
private, etc.). It simplifies complex systems by focusing only on what an object does rather than
how it does it.
4. Encapsulation
Encapsulation is the concept of wrapping data and methods into a single unit, i.e., the
class. It protects data from outside interference and misuse. Access to the data is controlled
through access specifiers and member functions. This is key to building secure and maintainable
programs.
5. Polymorphism
6. Inheritance
Inheritance is the process by which one class (child/derived class) acquires the properties
and behaviors of another class (parent/base class). It promotes code reusability and establishes a
relationship between classes. C++ supports various types of inheritance like single, multiple,
multilevel, hierarchical, and hybrid.
7. Data Hiding
Data hiding is the practice of keeping class data members private and allowing access only
through public functions. This ensures that sensitive data is not directly accessible, helping
enforce data security and integrity.
Introduction to C++
#include<iostream>
using namespace std;
int main() {
cout << "Hello, World!";
return 0;
}
In C++, data types define the type of data a variable can hold. Data types are crucial
because they tell the compiler how much memory to allocate and what kind of operations can be
performed on the data.
Note: The actual memory size can vary slightly depending on the system and compiler.
These are types created by the user to represent real-world entities more effectively.
class Same as struct but with access control class Employee { ... };
union Shares memory among members (only one active) union Data { int i; float f; }
enum Enum represents named integer constants enum Color { RED, GREEN };
typedef / using Creates a new name (alias) for an existing type typedef int Marks;
4. Void Data Type
C++ allows modifying basic data types using the following keywords:
You can convert one data type to another using type casting.
int a = 10;
float b = (float)a; // Type casting int to float
Operators in C++
Arithmetic +, -, *, /, % a + b, x % y
Logical &&, `
Comma , x = (a = 5, a + 3)
Control Statements in C++
1. Conditional Statements
switch Statement
int day = 2;
switch(day) {
case 1: cout << "Monday"; break;
case 2: cout << "Tuesday"; break;
default: cout << "Invalid";
}
2. Looping Statements
for loop
while loop
int i = 1;
while (i <= 5) {
cout << i << " ";
i++;
}
do-while loop
int i = 1;
do {
cout << i << " ";
i++;
} while (i <= 5);
3. Jump Statements
Statement Description
C++ uses cin and cout for taking input and giving output.
They are part of the iostream library.
int age;
cout << "Enter your age: ";
cin >> age;
Input and Output chaining:
int x, y;
cin >> x >> y;
cout << "Sum: " << x + y;
Dynamic Initialization
Definition:
Dynamic initialization means initializing variables or objects at runtime using values that are
computed during execution.
This is useful when the exact value is not known at compile time.
Example:
#include <iostream>
using namespace std;
int main() {
int a = 5, b = 10;
int sum = a + b; // Dynamic initialization using expression
cout << "Sum: " << sum;
return 0;
}
Reference Variables
Definition:
A reference variable is an alias (another name) for an existing variable. It is declared using the
& symbol.
Syntax:
int a = 10;
int &ref = a; // ref is now another name for a
ref = 20; // this also changes a
Key Points:
Function Example:
Class:
A class is a user-defined data type that holds data members (variables) and member functions
(methods). It defines the structure and behavior of objects.
Object:
An object is an instance of a class. It uses the class definition to hold actual values.
Class Specification
Syntax:
class ClassName {
private: // Access modifier
// data members
public:
// member functions
};
Access Specifiers:
class Student {
private:
int roll;
string name;
public:
void getData(); // Declaration
void display() { // Inline definition
cout << "Roll: " << roll << ", Name: " << name << endl;
}
};
// Member function defined outside the class
void Student::getData() {
cout << "Enter Roll and Name: ";
cin >> roll >> name;
}
Usage:
int main() {
Student s1;
s1.getData();
s1.display();
return 0;
}