0% found this document useful (0 votes)
9 views16 pages

Unit 1 Notes C++

The document provides an overview of C++ programming, covering its features such as object-oriented programming, data abstraction, and input/output operations. It explains data types, including built-in, derived, and user-defined types, along with pointers and type conversion methods. Additionally, it includes examples of calculator programs and demonstrates the use of input/output operators in C++.

Uploaded by

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

Unit 1 Notes C++

The document provides an overview of C++ programming, covering its features such as object-oriented programming, data abstraction, and input/output operations. It explains data types, including built-in, derived, and user-defined types, along with pointers and type conversion methods. Additionally, it includes examples of calculator programs and demonstrates the use of input/output operators in C++.

Uploaded by

renh74467
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Unit 1

1. FEATURES OF C++

2. DATA ABSTRACTION

[Link] AND OUTPUT

[Link] TYPES -> DERIVED AND USER DEFINED DATA TYPES

5. POINTERS , DECLARATION OF VARIABLE

6. CALCULATOR PROGRAM USING CLASS IN C++

7. TYPE CONVERSION IMPLICIT AND EXPLICIT WITH EXAMPLES…

1. FEATURES OF C++:

Object-Oriented Programming (OOP)

Object-Oriented Programming (OOP) is a programming paradigm


based on the concept of objects. Objects contain data and functions.
The data is called attributes or properties, and the functions are called
methods. OOP organizes software design around objects instead of
functions and logic. An object represents a real-world entity with its own
data and behavior.

Top Features of C++

1. Simple

C++ is considered a simple programming language, especially for


programmers who already know the C language. It follows a structured
programming approach, which helps programmers divide large
problems into smaller modules. This makes the program easier to
understand and maintain.

2. Object-Oriented

C++ supports object-oriented programming concepts which help in easier


program development and maintenance. Important OOP concepts include:

 Polymorphism – One function or object can take many forms.

 Inheritance – One class can acquire properties and methods from


another class.

 Encapsulation – Combining data and functions into a single unit


called a class.
 Abstraction – Showing only important information and hiding
implementation details.

 Data Hiding – Protecting data from unauthorized access.

3. Machine Independent and Platform Dependent

C++ programs can run on different machines with little modification,


which makes it machine independent. However, the compiled file
depends on the operating system, so it is platform dependent.

4. Popular Language

C++ is widely used in many fields such as game development,


operating systems, browsers, and large business applications
because of its high speed and strong memory management.

5. Case Sensitive

C++ is a case-sensitive language, meaning uppercase and lowercase


letters are treated differently.
For example, cout, Cout, and COUT are considered different.

6. Mid-Level Programming Language

C++ is called a mid-level language because it combines features of


both low-level languages (machine level) and high-level languages
(human-readable).

7. Structured Programming Language

C++ allows programs to be divided into smaller parts using functions,


classes, and objects. This modular structure makes the code more
organized and easier to maintain.

8. Rich Library

C++ provides a large number of built-in library functions which help


programmers perform tasks easily. Examples include:

 <iostream> – Input and output operations

 <cmath> – Mathematical functions

 <fstream> – File handling operations

 <algorithm> – Data manipulation functions

9. Memory Management

C++ supports dynamic memory allocation, which means memory can


be allocated during runtime. Memory in C++ is divided into stack
memory and heap memory. The operators new and delete are used to
allocate and free memory.

10. Powerful and Fast

C++ is a very powerful and fast programming language. It supports


multiple programming styles such as procedural programming, object-
oriented programming, and generic programming.

11. Pointer Support

C++ supports pointers, which store the address of another variable in


memory. Pointers are useful for memory management, arrays, structures,
and functions.

Example:

int *pointVar;

12. Recursion

Recursion is a technique where a function calls itself repeatedly until a


certain condition is satisfied. It helps reduce the length of the code and
improves readability.

13. Integration and Extensibility

C++ supports the development of large and complex applications. It


allows integration of new features and technologies easily.

14. Compiler-Based Language

C++ is a compiler-based language, meaning the program must be


compiled before execution. The compiler converts the code into machine
language that the computer can understand.

15. Multithreading

C++ supports multithreading, which allows multiple threads or tasks of


a program to run simultaneously, improving performance and efficiency.

2. I/O Operations in C++

Every program takes input data, processes it, and produces output. This
is called the Input → Process → Output cycle. Therefore, it is important
to know how to provide input to a program and display the results.

In C++, Input and Output (I/O) operations are performed using the
cin and cout streams along with the operators >> and <<.
 >> → Input operator (Extraction operator)

 << → Output operator (Insertion operator)

1. Input Operator (>>)

The input operator (>>) is also called the extraction operator. It is


used with the standard input stream cin to take input from the user.

The operator works with two operands:

 cin on the left side

 variable on the right side

It extracts data from the input stream and stores it in the


variable.

Example Program

#include <iostream>
using namespace std;

int main()
{
int a;
cin >> a; // taking input
a = a + 1;
return 0;
}

In this program, the statement cin >> a takes input from the user and
stores it in variable a.

2. Output Operator (<<)

The output operator (<<) is also called the insertion operator. It is


used with the standard output stream cout to display output on the
screen.

The operator works with two operands:

 cout on the left side

 value or expression on the right side

It inserts data into the output stream and displays it on the


screen.
Example Program

#include <iostream>
using namespace std;

int main()
{
int a;
cin >> a;
a = a + 1;
cout << a; // displaying output
return 0;
}

Here, cout << a displays the value of variable a on the screen.

Cascading of Input/Output Operators

Cascading means using multiple input or output operators in a


single statement.

Example without Cascading

#include <iostream>
using namespace std;

int main()
{
int a, b;
cin >> a;
cin >> b;

cout << "The value of a is ";


cout << a;
cout << "The value of b is ";
cout << b;

return 0;
}

Example with Cascading

#include <iostream>
using namespace std;
int main()
{
int a, b;
cin >> a >> b;

cout << "The value of a is " << a;


cout << "The value of b is " << b;

return 0;
}

In this example:

 cin >> a >> b takes two inputs in one statement.

 cout << "The value of a is " << a prints text and variable
together.

Cascading makes the program shorter, cleaner, and easier to read.

[Link] TYPES:

Data Types in C++

A data type defines the type of data a variable can store and the
operations that can be performed on that data. In C++, each data
type is stored differently in the computer’s memory.

C++ provides three main categories of data types:

1. Built-in (Fundamental) Data Types

2. Derived Data Types

3. User-defined Data Types

1. Built-in Data Types

Built-in data types are the basic data types provided by C++. These
include:

 Integral Data Types

 Floating Point Data Types

 Void Data Type

C++ also provides additional types like bool and wchar_t.


Type modifiers such as short, long, signed, and unsigned can be used
to change the size or range of some data types.

Integral Data Types

Integral data types are used to store whole numbers (integers).

1. Char

The char data type stores a single character such as letters, numbers, or
special symbols.

Example:

'A', '5', '@'

 Size: 1 byte

 Range: -128 to 127

Characters are stored internally as ASCII integer values.

2. Int

The int data type stores integer values without decimal points.

Example:

4, 42, -32, 5233

It cannot store fractional numbers like 4.28.

Modifiers used with int:

 short int

 long int

 signed int

 unsigned int

Floating Point Data Types

Floating point data types store real numbers (numbers with decimal
points).

Examples:

3.28, -24.53, 64.75


Types include:

 float

 double

Example:

float price = 25.75;


double salary = 12345.678;

Void Data Type

The void data type represents no value.

Uses:

 To specify a function does not return any value

 To specify a function does not take parameters

Example:

void display();

Void cannot be used to declare variables because it does not store


data.

Bool Data Type

The bool data type stores Boolean values.

Possible values:

 true (1)

 false (0)

Example:

bool result = true;

2. Derived Data Types

Derived data types are created from built-in data types.

Examples:

Array
An array is a collection of elements of the same data type stored in
contiguous memory locations.

Example:

int arr[5];

Function

A function is a block of code that performs a specific task and can be


called when required.

Example:

void display();

Reference

A reference is another name (alias) for a variable.

Example:

int a = 10;
int &b = a;

Both a and b refer to the same memory location.

Pointer

A pointer stores the memory address of another variable.

Example:

int *ptr;

Pointers help in dynamic memory allocation.

3. User-defined Data Types

User-defined data types are created by the programmer.

Examples:

 Structure

 Union

 Enumeration
 Class

Structure

A structure groups variables of different data types under one name.

Example:

struct student
{
int id;
char name[20];
};

Union

A union is similar to a structure but all members share the same


memory location.

Class

A class is a user-defined data type used in object-oriented


programming.
It contains data members and member functions.

Example:

class Student
{
int id;
void display();
};

Enumerated Types (enum):

An enumeration is a user-defined data type that assigns names to


integer constants.

Example:

enum color { red, green, blue };

Here:

 red = 0
 green = 1

 blue = 2

Example with custom value:

enum color { red, green = 5, blue };

Here:

 red = 0

 green = 5

 blue = 6

typedef Declaration

The typedef keyword is used to create a new name for an existing


data type.

Syntax:

typedef type newname;

Example:

typedef int feet;


feet distance;

Here, feet is another name for int.

[Link]:
Pointers in C++

A pointer is a variable that stores the memory address of another


variable. Instead of storing a value directly, a pointer stores the location
where the value is stored in memory.

Pointers are very useful for dynamic memory allocation, arrays,


structures, and functions.

Declaration of a Pointer

A pointer is declared using the asterisk (*) symbol.

Syntax

datatype *pointerName;
Example

int *ptr;

Here:

 int → type of variable whose address will be stored

 ptr → pointer variable

Example Program of Pointer

#include <iostream>
using namespace std;

int main()
{
int a = 10;
int *ptr;

ptr = &a; // storing address of a

cout << "Value of a: " << a << endl;


cout << "Address of a: " << &a << endl;
cout << "Pointer value: " << ptr << endl;
cout << "Value using pointer: " << *ptr << endl;

return 0;
}

Explanation

 &a → address of variable a

 ptr → stores the address of a

 *ptr → accesses the value stored at that address

So:

 ptr gives address

 *ptr gives value stored at that address


Pointer Operators

Two important operators are used with pointers:

Operat
Meaning
or

Address-of operator (gives memory


&
address)

Dereference operator (gives value at


*
address)

Example:

int a = 5;
int *p = &a;

Advantages of Pointers

1. Allow dynamic memory allocation.

2. Help in efficient memory usage.

3. Used in arrays, structures, and functions.

4. Useful for passing arguments by reference.

5. Improve program performance.

Example

int x = 5;
int *p = &x;

Memory representation:

x=5
Address of x = 1000

p = 1000
*p = 5
5. Type Conversion in C++:
Type conversion is the process of converting a variable from one
data type to another data type. It is commonly used when performing
operations on variables that have different data types.

For example, if we add an integer and a floating-point number, the


integer is converted to a floating-point number so the calculation can be
done correctly.

Types of Type Conversion in C++

There are two types of type conversion in C++:

1. Implicit Type Conversion

2. Explicit Type Conversion (Typecasting)

1. Implicit Type Conversion

Implicit type conversion is automatically performed by the compiler.


The compiler converts one data type into another without the programmer
specifying it.

Example Program:

#include <iostream>
using namespace std;

int main()
{
int int_var;
float float_var = 20.5;

int_var = float_var;

cout << "The value of int_var is: " << int_var << endl;
cout << "The value of float_var is: " << float_var << endl;

return 0;
}
Output

The value of int_var is: 20


The value of float_var is: 20.5

Here, the floating-point value 20.5 is converted to 20 when stored in the


integer variable. The decimal part is removed automatically.

2. Explicit Type Conversion (Typecasting)

Explicit type conversion is performed manually by the programmer.


In this type of conversion, the programmer forces a variable to change
its data type.

This is also called typecasting.

Explicit conversion is used when the programmer does not want to follow
the default implicit conversion rules.

Explicit type conversion can be done in two ways:

 Using Assignment Operator

 Using Cast Operator

Conversion Using Cast Operator

Example Program:

#include <iostream>
using namespace std;

int main()
{
char char_var = 'a';
int int_var;

int_var = (int) char_var;

cout << "The value of char_var is: " << char_var << endl;
cout << "The value of int_var is: " << int_var << endl;

return 0;
}
Output

The value of char_var is: a


The value of int_var is: 97

You might also like