0% found this document useful (0 votes)
16 views17 pages

Oop 1.3

Presentation

Uploaded by

antarasartale11
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
0% found this document useful (0 votes)
16 views17 pages

Oop 1.3

Presentation

Uploaded by

antarasartale11
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/ 17

Object Oriented

Programming Using C++


UNIT 1- PRINCIPLES OF OBJECT ORIENTED PROGRAMMING
1.3
Contents:
1. Data types

2. Declaration of Variables

3. Constants

4. Dynamic initialization of variable

5. Reference variable

6. Type Compatibility

7. Type casting/ Type conversion


Data types:
1. A data type specifies the type of data that a variable can store.
2. Each data type requires different amounts of memory and has some specific operations
which can be performed over it.
Data type Memory Requirement Range

char 1 byte 0 to 255

int 2 or 4 byte -2147483648 to


+ 2147483647
Boolean 1 byte true or false

float 4 byte -3.4*10^38 to


3.4*10^38

double 8 byte
C++ Datatypes

2. Derived Datatypes 3. User defined Data types


1. Predefined Datatypes • These are the data types
• These are the datatypes which is • These are the data types which
which can create with the user can create as per user
already defined. help of Predefined
• E.g: int, requirement.
datatypes.
Boolean,float,double,etc • E.g:Function, Array • E.g. Class, structure, union ,etc.
pointers.
Declaration of Variable:
• Variable is a named location in a memory where a program can manipulate the data. This location is used to hold the
value of the variable.

• The value of the variable may get change in the program.

• variable might be belonging to any of the data type like int, float, char etc.

• Syntax:
datatype variable_list;

• The example of declaring the variable is given below:


• int a;
• float b;
• char c;
• Here, a, b, c are variables. The int, float, char are the data types.
Constants:
• Constants are the values that is given to identifiers, and that value not change
throughout the program execution .

• Constants are fixed values.

• There are two types to define constant:

1.By using # define.


2.By using const keyword.
• Syntax of constant using #define:

#define
#define identifier
identifier name
name value value

• Syntax of constant using const keyword:

const datatype variable name=value;


Dynamic initialization of variable:
• When the value of variable is initialized when it is declared, it is known as Dynamic initialization of variable.

• Variables are assigned value at the runtime.


Data type use here is int,it will
• Example: take 4 bytes of memory.
• int a=10; Operating system will allocate 4
bytes of memory for the variable a
.
Data type Dynamic Initialize And the dynamic initialize value
variable name her is 10, this value will be stored
value of the variable
in the 4 bytes of memory that
Operating system have allocated
for int data type.
Reference Variable:
• Reference variable is an alternate name of already existing variable.

• The operator ‘&’ is used to declare reference variable.

• It is a variable with different name but having same memory location.

• Reference variable are important for working with objects & creating dynamic,flexible
programs.

• Unlike primitive data types that hold their values directly, reference variables store the
address of the object they refer to, enabling the manipulation of objects through these
references.

• Syntax of reference variable:

datatype variablename=value; // variable declaration


datatype &reference _variable=variablename; // reference variable.
Example of Reference Variable:
#include<iostream.h>
OUTPUT
#include<conio.h> The value of the variable a is:10
OUTPUT
The value of the reference variable is: 10
void main()
{
int a=10;
int&b=a;
cout<<“The value of the variable a is:”<<a;
cout<<“The value of the reference variable is:”<<b;
getch();
};
Type Compatibility:
• Type compatibility is the process that converts the predefined data type of one variable into an
appropriate data type.

• Type compatibility is use to convert two different data type variables into a single data type to
solve mathematical and logical expressions easily without any data loss.

• Example:
• int a= 10.5;

integer data type Float data types ,


memory req- memory req- 4 bytes
4 bytes
Type casting/ Type Conversion:

• A Type casting or Type Conversion is basically a conversion from one


Primitive datatype to another Primitive data type.

• There are two types of type casting:


1. Implicit Type casting/automatic type casting
2. Explicit Type casting.
1. Implicit Type casting\ automatic type casting:
• It is an automatic type casting, compiler will automatically
change one type of data into other.

• No data loss.

• Conversion of operand i.e. lower data type to higher data


type.
Program of implicit type:
Write a program to take one input from user as int data type and one float data type. Multiply the two numbers
and the the result.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr(); OUTPUT
int a;
float b;result; Enter one integer number:4
cout<<“ Enter one integer number:”;
cin>>a; Enter one float number:3.9
cout<<“ Enter one float number:”;
cin>>b; The Multiplication of two numbers is:15.6
result=a*b;
cout<<“The Multiplication of two numbers is:”<<result;
getch();
};
2. Explicit Type casting:
• It is used to convert higher data types to lower data types.

• Loss of memory is there as higher data type is converted into lower data type.

• Explicit type conversion in C++ is the process of manually converting one data
type to another data type using casting operators.

• It is also called as typecasting.

• Syntax:
Datatype variable_name=(datatype) variable_name;
Program of explicit type conversion :
Write a program to accept one value as double int data type and explicitly convert it into integer and print the
result.
#include<iostream.h>
#include<conio.h>
int main() OUTPUT
{ Enter one double value:56.7
clrscr(); Value of double value is:56.7
double d; Value of integer value is:56
cout<<“Enter one double value:”;
cin>>d;
int a=(int)d;
cout<<“ value of double value is:”<<d;
cout<<“value of integer value is:”<<a;
return 0;
};
THANK YOU

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy