Thanks to visit codestin.com
Credit goes to www.scribd.com

100% found this document useful (1 vote)
214 views10 pages

Unit-1 - C++ Programming Basic

This document provides an overview of basic C++ programming concepts, focusing on object-oriented programming principles such as classes, objects, abstraction, encapsulation, polymorphism, inheritance, and data hiding. It also covers the structure of a C++ program, data types, operators, control statements, input/output operations, dynamic initialization, and reference variables. Additionally, it explains class specifications and member function definitions, highlighting the importance of access specifiers and the organization of code.

Uploaded by

sangeethamtech26
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
214 views10 pages

Unit-1 - C++ Programming Basic

This document provides an overview of basic C++ programming concepts, focusing on object-oriented programming principles such as classes, objects, abstraction, encapsulation, polymorphism, inheritance, and data hiding. It also covers the structure of a C++ program, data types, operators, control statements, input/output operations, dynamic initialization, and reference variables. Additionally, it explains class specifications and member function definitions, highlighting the importance of access specifiers and the organization of code.

Uploaded by

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

UNIT-1 C++ PROGRAMMING BASIC

Object oriented programming concepts: Class – Object - Abstraction – Encapsulation –


Polymorphism-inheritance- Data Hiding, Introduction to C++:Structure of C++ program,
Data types, Operators and control statements, Input and output operators, Dynamic
initialization, Reference variables, Classes and Objects: Class specification- Member
function definition

Object-Oriented Programming Concepts

1. Class

A class is a fundamental concept in object-oriented programming. It is a user-defined data


type that represents a real-world entity with properties (data members) and behaviors (member
functions). A class serves as a blueprint for creating objects. For example, a Student class may
have attributes like name and roll number, and functions like display().

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

Polymorphism means "many forms". It allows one function or operator to behave


differently based on the context. In C++, polymorphism is of two types:

 Compile-time (Function Overloading, Operator Overloading)


 Run-time (Function Overriding using Virtual Functions)

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++

1. Structure of a C++ Program

A basic C++ program contains:

 Header files (#include<iostream>)


 main() function as the entry point
 Code enclosed in {} braces
Example:

#include<iostream>
using namespace std;
int main() {
cout << "Hello, World!";
return 0;
}

Data Types in C++

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.

Categories of Data Types in C++

C++ data types can be broadly classified into four categories:

1. Basic (or Fundamental) Data Types


2. Derived Data Types
3. User-defined Data Types
4. Void Type

1.Basic (Fundamental) Data Types

Data Type Description Example Memory Size (approx.)

int Integer numbers int a = 10; 4 bytes

float Floating-point numbers (decimals) float x = 2.5; 4 bytes


Data Type Description Example Memory Size (approx.)

double Double-precision floating-point double pi = 3.1415; 8 bytes

char Single character char ch = 'A'; 1 byte

bool Boolean value (true or false) bool flag = true; 1 byte

Note: The actual memory size can vary slightly depending on the system and compiler.

2. Derived Data Types

These are built from fundamental types:

Data Type Description Example

Array Collection of elements of the same type int a[5];

Pointer Holds address of another variable int *p = &a;

Function A block of code that performs a task int sum(int a, int b)

Reference Alias for another variable int &ref = a;

3.User-defined Data Types

These are types created by the user to represent real-world entities more effectively.

Data Type Description Example

struct Groups variables of different types struct Student {int id;}

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

Data Type Description Example

void Represents "no value" or "empty" void show(); or return;

 Used for functions that do not return a value.


 Also used with pointers as void *ptr; to point to any data type (generic pointer).

Type Modifiers in C++

C++ allows modifying basic data types using the following keywords:

Modifier Description Example

signed Allows both positive and negative signed int x = -10;

unsigned Only positive values unsigned int y = 10;

short Reduces size (e.g., 2 bytes int) short int a;

long Increases size long int b;

Examples of Modified Types:

 unsigned char → 0 to 255


 long double → extended precision for decimals

Memory Chart (Typical for 32-bit/64-bit systems)

Data Type Size (32-bit) Range (Approx.)

char 1 byte -128 to 127

unsigned char 1 byte 0 to 255

int 4 bytes -2,147,483,648 to 2,147,483,647

unsigned int 4 bytes 0 to 4,294,967,295

float 4 bytes ±3.4e38 (7 digits precision)

double 8 bytes ±1.7e308 (15 digits precision)

bool 1 byte true / false


Type Casting in C++

You can convert one data type to another using type casting.

int a = 10;
float b = (float)a; // Type casting int to float

Use in Real-World Programs

 int → counts, loop counters


 float/double → measurements, percentages, scientific values
 char → DNA bases (A, T, G, C), characters
 bool → flags, decision making (e.g., valid/invalid)
 struct/class → for modeling biological entities (e.g., Gene, Protein)

Operators in C++

Operators are symbols that perform operations on variables and values.

Types of Operators in C++:

Category Operators Used Example

Arithmetic +, -, *, /, % a + b, x % y

Relational ==, !=, <, >, <=, >= a == b, x > y

Logical &&, `

Assignment =, +=, -=, *=, /=, %= a += 5, x *= 2

Increment/Decrement ++, -- a++, --x

Bitwise &, ` , ^, ~, <<, >>`

Ternary (Conditional) ?: a>b?a:b

Scope Resolution :: std::cout

Sizeof sizeof() sizeof(int)

Comma , x = (a = 5, a + 3)
Control Statements in C++

Control statements control the flow of execution in a program.

Types of Control Statements:

1. Conditional Statements

Used to make decisions based on conditions.

 if, if-else, else-if, nested if

if (age >= 18) {


cout << "Eligible to vote";
} else {
cout << "Not eligible";
}

 switch Statement

Used to select one of many code blocks to execute.

int day = 2;
switch(day) {
case 1: cout << "Monday"; break;
case 2: cout << "Tuesday"; break;
default: cout << "Invalid";
}

2. Looping Statements

Used to execute a block of code repeatedly.

for loop

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


cout << i << " ";
}

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

Used to jump from one part of code to another.

Statement Description

break Exits the loop or switch early

continue Skips the current iteration

return Exits from a function

goto Jumps to a labeled statement (not recommended)

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


if (i == 3) continue;
cout << i << " ";
}

Input and Output Operators in C++

C++ uses cin and cout for taking input and giving output.
They are part of the iostream library.

cout → Output Operator (<<)

Displays information on the screen.

cout << "Welcome to C++";


int a = 5;
cout << "Value of a is: " << a;

cin → Input Operator (>>)

Takes input from the user via keyboard.

int age;
cout << "Enter your age: ";
cin >> age;
Input and Output chaining:

You can chain multiple inputs and outputs.

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:

 Must be initialized at declaration.


 Useful for function parameter passing by reference.

Function Example:

void swap(int &x, int &y) {


int temp = x;
x = y;
y = temp;
}

Classes and Objects

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:

 private: accessible only within the class


 public: accessible outside the class
 protected: accessible in derived classes

Member Function Definition

Member functions can be defined:

 Inside the class (implicitly inline)


 Outside the class using the scope resolution operator ::

Example (inside and outside definition):

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;
}

You might also like