Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Iit Delhi 22

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 76

Introduction to By: Geeta Kakrani

C++ Kanishka IT Pvt.Ltd.


Overview:-

✓ Beginning with C++


✓ Explaining Object Oriented Programming(C++)
✓ C++ Tokens, Expressions, and Control Structure
✓ Functions In C++
✓ String Manipulation in C++
✓ Classes And Objects
✓ Memory Management and pointers
Cont..

✓ Constructor and Destructors


✓ Operator Overloading and type conversion
✓ Inheritance – extending class
✓ Pointer, Virtual Functions, Polymorphism.
✓ Working with files
✓ Exception Handling
1. What is C++, its Applications, Advantages etc.

C++ is a general-purpose object-oriented programming (OOP)


language, developed by Bjarne Stroustrup, and is an extension of the C
language. It is therefore possible to code C++ in a "C style" or "object-
oriented style." In certain scenarios, it can be coded in either way and is
thus an effective example of a hybrid language.
C++ is considered to be an intermediate-level language, as it
encapsulates both high- and low-level language features. Initially, the
language was called "C with classes" as it had all the properties of the
C language with an additional concept of "classes." However, it was
renamed C++ in 1983.
It is pronounced as "see-plus-plus."
Difference between C and C++.

C C++
C was developed by Dennis Ritchie C++ was developed by Bjarne
between the year 1969 and 1973 at Stroustrup in 1979.
AT&T Bell Labs.
C does no support polymorphism, C++ supports polymorphism,
encapsulation, and inheritance encapsulation, and inheritance
which means that C does not because it is an object oriented
support object oriented programming language.
programming.
scanf() and printf() functions are cin and cout are used
used for input/output in C. for input/output in C++.
For the development of code, C C++ is known as hybrid language
supports procedural programming. because C++ supports
both proceduraland object
oriented programming paradigms.
Data and functions are separated Data and functions are
in C because it is a procedural encapsulated together in form of
programming language. an object in C++.
C C++
Functions in C are not defined Functions can be used inside a
inside structures. structure in C++.
Virtual and friend functions are not Virtual and friend functions are
supported by C. supported by C++.
C does not support inheritance. C++ supports inheritance.
C provides malloc() and calloc() C++ provides new operator for
functions for dynamic memory memory allocation and delete
allocation, and free() for memory operator for memory de-allocation.
de-allocation.
2. Explaining Object Oriented
Language.(C++)
OBJECT-ORIENTED PROGRAMMING (OOP) IS THE TERM
USED TO DESCRIBE A PROGRAMMING APPROACH BASED
ON OBJECTS AND CLASSES. THE OBJECT-ORIENTED
PARADIGM ALLOWS US TO ORGANIZE SOFTWARE AS A
COLLECTION OF OBJECTS THAT CONSIST OF BOTH DATA
AND BEHAVIOUR. THIS IS IN CONTRAST TO CONVENTIONAL
FUNCTIONAL PROGRAMMING PRACTICE THAT ONLY
LOOSELY CONNECTS DATA AND BEHAVIOUR.
Features of OOP
1. CLASS AND OBJECTS
2. INHERITANCE
3. POLYMORPHISM
4. ABSTRACTION
5. ENCAPSULATION
6. EXCEPTION HANDLING
Class
THE BUILDING BLOCK OF C++ THAT LEADS TO OBJECT ORIENTED PROGRAMMING IS
A CLASS. IT IS A USER DEFINED DATA TYPE, WHICH HOLDS ITS OWN DATA MEMBERS AND
MEMBER FUNCTIONS, WHICH CAN BE ACCESSED AND USED BY CREATING AN INSTANCE OF
THAT CLASS. A CLASS IS LIKE A BLUEPRINT FOR AN OBJECT.

OBJECTS
AN OBJECT IS AN INSTANCE OF A CLASS. WHEN A CLASS IS DEFINED, NO MEMORY IS
ALLOCATED BUT WHEN IT IS INSTANTIATED (I.E. AN OBJECT IS CREATED) MEMORY IS
ALLOCATED. EG: PEN, PENCIL, INKPOT ETC.
Example
CONSIDER THE CLASS OF CARS. THERE MAY BE MANY CARS WITH
DIFFERENT NAMES AND BRAND BUT ALL OF THEM WILL SHARE SOME
COMMON PROPERTIES LIKE ALL OF THEM WILL HAVE 4
WHEELS, SPEED LIMIT, MILEAGE RANGE ETC. SO HERE, CAR IS THE
CLASS AND WHEELS, SPEED LIMITS, MILEAGE ARE THEIR PROPERTIES
OBJECTS WILL BE TYRES, ENGINE, HEADLIGHT, MIRRORS, PAINT ETC.
Declaration of Class and Objects
Class Declerartion

Object Decleration
ClassName ObjectName;
Inheritance
WHEN ONE OBJECT ACQUIRES ALL THE PROPERTIES AND BEHAVIOURS
OF PARENT OBJECT I.E. KNOWN AS INHERITANCE. IT PROVIDES CODE
REUSABILITY. IT IS USED TO ACHIEVE RUNTIME POLYMORPHISM.
Polymorphism

WHEN ONE TASK IS PERFORMED BY DIFFERENT WAYS I.E. KNOWN AS


POLYMORPHISM. FOR EXAMPLE: TO CONVINCE THE CUSTOMER DIFFERENTLY,
TO DRAW SOMETHING E.G. SHAPE OR RECTANGLE ETC.
THERE ARE TWO TYPES OF POLYMORPHISM
1. COMPILE-TIME POLYMORPHISM (FUNCTION OVERLOADING).
2. RUN-TIME POLYMORPHISM (FUNCTION OVERRIDING).
Abstraction

HIDING INTERNAL DETAILS AND SHOWING


FUNCTIONALITY IS KNOWN AS ABSTRACTION.
FOR EXAMPLE: PHONE CALL, WE DON'T KNOW
THE INTERNAL PROCESSING.
IN C++, WE USE ABSTRACT CLASS AND
INTERFACE TO ACHIEVE ABSTRACTION.
Encapsulation

BINDING (OR WRAPPING) CODE AND DATA TOGETHER INTO A SINGLE


UNIT IS KNOWN AS ENCAPSULATION. FOR EXAMPLE: CAPSULE, IT IS
WRAPPED WITH DIFFERENT MEDICINES.
Advantage of OOPs over Procedure-
oriented programming language

• OOPS MAKES DEVELOPMENT AND MAINTENANCE EASIER WHERE


AS IN PROCEDURE-ORIENTED PROGRAMMING LANGUAGE IT IS
NOT EASY TO MANAGE IF CODE GROWS AS PROJECT SIZE
GROWS.
• OOPS PROVIDE DATA HIDING WHEREAS IN PROCEDURE-ORIENTED
PROGRAMMING LANGUAGE A GLOBAL DATA CAN BE ACCESSED
FROM ANYWHERE.
• OOPS PROVIDE ABILITY TO SIMULATE REAL-WORLD EVENT MUCH
MORE EFFECTIVELY. WE CAN PROVIDE THE SOLUTION OF REAL
WORD PROBLEM IF WE ARE USING THE OBJECT-ORIENTED
PROGRAMMING LANGUAGE.
3. C++ Tokens, Expressions, and
Control Structure
AS WE KNOW C++ IS A SUPERSET OF C AND THEREFORE MOST
CONSTRUCTS OF C ARE LEGAL IN C++. BUT THERE ARE SOME
EXCEPTIONS AND ADDITIONS. IN THIS UNIT, WE SHALL DISCUSS THESE
EXCEPTIONS AND ADDITIONS IN C++.
Tokens
A TOKEN IS THE SMALLEST ELEMENT OF A C++ PROGRAM THAT IS
MEANINGFUL TO THE COMPILER. THE C++ PARSER RECOGNIZES THESE
KINDS OF TOKENS: IDENTIFIERS, KEYWORDS, LITERALS, OPERATORS,
PUNCTUATORS, AND OTHER SEPARATORS. A STREAM OF THESE TOKENS
MAKES UP A TRANSLATION UNIT.
TOKENS ARE USUALLY SEPARATED BY WHITE SPACE. WHITE SPACE CAN
BE ONE OR MORE:
• BLANKS
• HORIZONTAL OR VERTICAL TABS
• NEW LINES
• COMMENTS
C++ keywords
A KEYWORD IS A RESERVED WORD. YOU CANNOT USE IT AS A VARIABLE NAME,
CONSTANT NAME ETC. A LIST OF 32 KEYWORDS IN C++ LANGUAGE WHICH ARE
ALSO AVAILABLE IN C LANGUAGE ARE GIVEN BELOW.

auto break case char const continu default do


e
double else enum extern float for goto if
int long registe return short signed sizeof static
r
struct switch typede union unsign void volatile while
f ed
Data Types
ALL VARIABLES USE DATA-TYPE DURING DECLARATION TO RESTRICT THE TYPE
OF DATA TO BE STORED. THEREFORE, WE CAN SAY THAT DATA TYPES ARE USED
TO TELL THE VARIABLES THE TYPE OF DATA IT CAN STORE. WHENEVER A
VARIABLE IS DEFINED IN C++, THE COMPILER ALLOCATES SOME MEMORY FOR
THAT VARIABLE BASED ON THE DATA-TYPE WITH WHICH IT IS DECLARED. EVERY
DATA TYPE REQUIRES A DIFFERENT AMOUNT OF MEMORY.

TYPES OF DATA TYPES


1. PRE-DEFINE – EG: INT CHAR FLOAT DOUBLE ETC.
2. USER-DEFINE – EG: CLASS, STRUCTURE, UNION, ENUM, TYPEDEF.
3. DERIVED: FUNCTION, ARRAY, POINTERS, REFERENCE.
DATA TYPE SIZE (IN BYTES) RANGE
short int 2 -32,768 to 32,767
unsigned short int 2 0 to 65,535
unsigned int 4 0 to 4,294,967,295

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

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

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


long long int 8 -(2^63) to (2^63)-1

unsigned long long int 8 0 to 18,446,744,073,709,551,615

signed char 1 -128 to 127


unsigned char 1 0 to 255
float 4
double 8
long double 12
wchar_t 2 or 4 1 wide character
C++ Operators
OPERATORS ARE USED TO PERFORM OPERATIONS ON VARIABLES AND
VALUES.
THE VALUE IS CALLED AN OPERAND, WHILE THE OPERATION (TO BE
PERFORMED BETWEEN THE TWO OPERANDS) IS DEFINED BY AN OPERATOR

C++ DIVIDES THE OPERATORS INTO THE FOLLOWING GROUPS:


• ARITHMETIC OPERATORS
• ASSIGNMENT OPERATORS
• COMPARISON OPERATORS
• LOGICAL OPERATORS
• BITWISE OPERATORS
Arithmetic Operators
ARITHMETIC OPERATORS ARE USED TO PERFORM COMMON
MATHEMATICAL OPERATIONS.

Operator Name Example


+ Addition x+y
- Subtraction x-y
* Multiplicatio x*y
n
/ Division x/y
% Modulus x%y
++ Increment ++x
-- Decrement --x
Assignment Operator Example Same As

Operators = x=5 x=5


+= x += 3 x=x+3
-= x -= 3 x=x-3
ASSIGNMENT OPERATORS ARE USED
TO ASSIGN VALUES TO VARIABLES. *= x *= 3 x=x*3
/= x /= 3 x=x/3
%= x %= 3 x=x%3
&= x &= 3 x=x&3
|= x |= 3 x=x|3
^= x ^= 3 x=x^3
>>= x >>= 3 x = x >> 3
<<= x <<= 3 x = x << 3
Comparison Operators
COMPARISON OPERATORS ARE USED TO COMPARE TWO VALUES.
TIP: THE RETURN VALUE IS EITHER TRUE (1) OR FALSE (0).

Operator Name Example


== Equal to x == y
!= Not equal x != y
> Greater than x>y
< Less than x<y
>= Greater than or equal x >= y
to
<= Less than or equal to x <= y
Logical Operators
LOGICAL OPERATORS ARE USED TO DETERMINE THE LOGIC BETWEEN
VARIABLES OR VALUES

Operator Name Description Example

&& Logical Returns true if both x < 5 && x < 10


and statements are true

|| Logical or Returns true if one of the x < 5 || x < 4


statements is true

! Logical not Reverse the result, returns !(x < 5 && x < 10)
false if the result is true
Bitwise Operators
BITWISE OPERATORS WORKS ON EACH BIT VALUE OF THE OPERANDS
AND GIVES THE RESULT.

Operator Name Discription Example


& bitwise AND Takes two numbers as operands and does AND on every x & y
bit of two numbers.
| bitwise OR Takes two numbers as operands and does OR on every x| y
bit of two numbers
^ bitwise XOR Takes two numbers as operands and does XOR on every x^ y
bit of two numbers.
<< left shift Takes two numbers, left shifts the bits of the first operand, x << y
the second operand decides the number of places to
shift.
>> right shift Takes two numbers, right shifts the bits of the first x>> y
operand, the second operand decides the number of
places to shift.
~ bitwise NOT Takes one number and inverts all bits of it ~x
Operator Precedence and Associativity
OPERATOR PRECEDENCE DETERMINES WHICH OPERATOR IS PERFORMED FIRST IN AN
EXPRESSION WITH MORE THAN ONE OPERATORS WITH DIFFERENT PRECEDENCE.
FOR EXAMPLE: SOLVE 10 + 20 * 30
10 + 20 * 30 IS CALCULATED AS 10 + (20 * 30) AND NOT AS (10 + 20) * 30

OPERATORS ASSOCIATIVITY IS USED WHEN TWO OPERATORS OF SAME


PRECEDENCE APPEAR IN AN EXPRESSION. ASSOCIATIVITY CAN BE
EITHER LEFT TO RIGHT OR RIGHT TO LEFT.
FOR EXAMPLE: ‘*’ AND ‘/’ HAVE SAME PRECEDENCE AND THEIR ASSOCIATIVITY
IS LEFT TO RIGHT, SO THE EXPRESSION “100 / 10 * 10” IS TREATED AS “(100 / 10) * 10”.
Rules to remember

1)ASSOCIATIVITY IS ONLY USED WHEN THERE ARE TWO OR MORE OPERATORS OF


SAME PRECEDENCE.
2) ALL OPERATORS WITH THE SAME PRECEDENCE HAVE SAME ASSOCIATIVITY.
3) PRECEDENCE AND ASSOCIATIVITY OF POSTFIX ++ AND PREFIX ++ ARE
DIFFERENT.
4) COMMA HAS THE LEAST PRECEDENCE AMONG ALL OPERATORS AND SHOULD
BE USED CAREFULLY.
5) THERE IS NO CHAINING OF COMPARISON OPERATORS IN C
Control Structures/ Statements
CONTROL STRUCTURES FORM THE BASIC ENTITIES OF A “STRUCTURED
PROGRAMMING LANGUAGE“. CONTROL STRUCTURES ARE USED TO
ALTER THE FLOW OF EXECUTION OF THE PROGRAM. TO MAKE OUR
PROGRAM ABLE TO MAKE DECISIONS FOR THAT PURPOSE WE USE
CONTROL STRUCTURES.

TYPES OF CONTROL STRUCTURES:


1) SELECTION STRUCTURE (ONE OR MANY BRANCHES) INCLUDES IF,
LADDER IF, NESTED IF AND SWITCH.
2)LOOP STRUCTURE (REPETITION OF A SET OF ACTIVITIES) INCLUDE FOR,
WHILE, DO WHILE LOOPS
Selection Structures
SELECTION STRUCTURES ARE USED TO
PERFORM ‘DECISION MAKING‘ AND THEN
BRANCH THE PROGRAM FLOW BASED ON THE
OUTCOME OF DECISION MAKING.
THERE ARE 4 SELECTION STATEMENTS.
1. IF STATEMENT
2. NESTED IF
3. LADDER IF
4. SWITCH STATEMENT

THE FLOW CHART SHOWS HOW SELECTION


STATEMENTS ARE IMPLEMENTED.
If statement If..else statement
IF (EXPRESSION) // THIS EXPRESSION IS if (expression) // This expression is
EVALUATED. IF EXPRESSION IS TRUE evaluated. If expression is TRUE
STATEMENTS INSIDE THE BRACES WILL BE statements inside the braces will be
EXECUTED executed
{ {
STATEMENT 1; statement 1;
STATEMENT 2; statement 2;
} }
STATEMENT 3; Else
{
Statement 3;
}
Ladder if
IF(EXPRESSION) // IF TRUE STATEMENT 1 WILL EXECUTE, OTHERWISE GOTO ELSE..IF
{
STATEMENT 1;
}
ELSE IF (EXPRESSION) // IF TRUE STATEMENT 2 WILL EXECUTE, OTHERWISE GOTO ELSE
{
STATEMENT 2;
}
ELSE // EXECUTED IF AND ONLY IF BOTH THE ABOVE EXPRESSION ARE FALSE
{
STATEMENT 3;
}
Nested If
IF (EXPRESSION)
{
IF (EXPRESSION)
{
STATEMENT 1;
}
ELSE
{
STATEMENT 2;
}
}
ELSE
{
STATEMENT 3;
}
Switch Statement
SWITCH(VARIABLE)
{
CASE 1:
STATEMENT 1;
BREAK;
CASE 2:
STATEMENT 2;
BREAK;
....
…..
…..
DEFAULT :
STATEMENT N;
}
Loop Structure
A LOOP STRUCTURE IS USED TO EXECUTE
A CERTAIN SET OF ACTIONS FOR A
PREDEFINED NUMBER OF TIMES OR
UNTIL A PARTICULAR CONDITION IS
SATISFIED.
THERE ARE 3 CONTROL STATEMENTS
AVAILABLE IN C/C++ TO IMPLEMENT
LOOP STRUCTURES.
1. FOR LOOP
2. WHILE LOOP
3. DO WHILE LOOP

THE FLOW CHART SHOWS HOW


SELECTION STATEMENTS ARE
IMPLEMENTED
While Loop
SYNTAX FOR WHILE LOOP IS SHOWN BELOW:
WHILE(CONDITION)// THIS CONDITION IS TESTED FOR TRUE OR FALSE.
STATEMENTS INSIDE CURLY BRACES ARE EXECUTED AS LONG AS CONDITION IS
TRUE
{
STATEMENT 1;
STATEMENT 2;
STATEMENT 3;
}
THE CONDITION IS CHECKED FOR TRUE FIRST. IF IT IS TRUE THEN ALL STATEMENTS
INSIDE CURLY BRACES ARE EXECUTED. THEN PROGRAM CONTROL COMES
BACK TO CHECK THE CONDITION HAS CHANGED OR TO CHECK IF IT IS STILL
TRUE. THE STATEMENTS INSIDE BRACES ARE EXECUTED REPEATEDLY, AS LONG
AS THE CONDITION IS TRUE. WHEN THE CONDITION TURNS FALSE, PROGRAM
CONTROL EXITS FROM WHILE LOOP.
NOTE:- WHILE IS AN ENTRY CONTROLLED LOOP. STATEMENT INSIDE BRACES ARE
ALLOWED TO EXECUTE ONLY IF CONDITION INSIDE WHILE IS TRUE.
Do while Loop
SYNTAX FOR DO WHILE LOOP IS SHOWN BELOW:
DO
{
STATEMENT 1;
STATEMENT 2;
STATEMENT 3;
}
WHILE(CONDITION);
UNLIKE WHILE, DO WHILE IS AN EXIT CONTROLLED LOOP. HERE THE SET OF
STATEMENTS INSIDE BRACES ARE EXECUTED FIRST. THE CONDITION INSIDE WHILE IS
CHECKED ONLY AFTER FINISHING THE FIRST TIME EXECUTION OF STATEMENTS INSIDE
BRACES. IF THE CONDITION IS TRUE, THEN STATEMENTS ARE EXECUTED AGAIN. THIS
PROCESS CONTINUES AS LONG AS CONDITION IS TRUE. PROGRAM CONTROL EXITS
THE LOOP ONCE THE CONDITION TURNS FALSE.
For Loop
SYNTAX OF FOR STATEMENT IS SHOWN BELOW:

FOR(INITIALIZATION; TEST CONDITION; UPDATION)


{
STATEMENT 1;
STATEMENT 2;
STATEMENT 3;
}

THE FOR STATEMENT IS AN ENTRY CONTROLLED LOOP. THE DIFFERENCE


BETWEEN WHILE AND FOR IS IN THE NUMBER OF REPETITIONS. THE FOR
LOOP IS USED WHEN AN ACTION IS TO BE EXECUTED FOR A PREDEFINED
NUMBER OF TIMES. THE WHILE LOOP IS USED WHEN THE NUMBER OF
REPETITIONS IS NOT PREDEFINED.
4. Functions In C++

A FUNCTION IS BLOCK OF CODE WHICH IS USED TO PERFORM A PARTICULAR TASK, FOR


EXAMPLE LET’S SAY YOU ARE WRITING A LARGE C++ PROGRAM AND IN THAT PROGRAM
YOU WANT TO DO A PARTICULAR TASK SEVERAL NUMBER OF TIMES, LIKE DISPLAYING VALUE
FROM 1 TO 10, IN ORDER TO DO THAT YOU HAVE TO WRITE FEW LINES OF CODE AND YOU
NEED TO REPEAT THESE LINES EVERY TIME YOU DISPLAY VALUES. ANOTHER WAY OF DOING
THIS IS THAT YOU WRITE THESE LINES INSIDE A FUNCTION AND CALL THAT FUNCTION EVERY
TIME YOU WANT TO DISPLAY VALUES. THIS WOULD MAKE YOU CODE SIMPLE, READABLE
AND REUSABLE.

Syntax of Function

return_type function_name (parameter_list)


{
//C++ Statements
}
Functions In C++ :-

Call by Reference :-
Inline Functions :-
Friend class and function

FRIEND CLASS A FRIEND CLASS CAN ACCESS PRIVATE AND


PROTECTED MEMBERS OF OTHER CLASS IN WHICH IT IS DECLARED AS
FRIEND. IT IS SOMETIMES USEFUL TO ALLOW A PARTICULAR CLASS TO
ACCESS PRIVATE MEMBERS OF OTHER CLASS.

FRIEND FUNCTION LIKE FRIEND CLASS, A FRIEND FUNCTION CAN BE


GIVEN SPECIAL GRANT TO ACCESS PRIVATE AND PROTECTED
MEMBERS. A FRIEND FUNCTION CAN BE:
• A METHOD OF ANOTHER CLASS
• A GLOBAL FUNCTION
Some math library functions
THE MATH.H HEADER DEFINES VARIOUS MATHEMATICAL FUNCTIONS AND ONE MACRO.
ALL THE FUNCTIONS AVAILABLE IN THIS LIBRARY TAKE DOUBLE AS AN ARGUMENT AND
RETURN DOUBLE AS THE RESULT. LET US DISCUSS SOME IMPORTANT FUNCTIONS ONE BY
ONE.

1. DOUBLE CEIL(DOUBLE X): THE C LIBRARY FUNCTION DOUBLE CEIL(DOUBLE X) RETURNS


THE SMALLEST INTEGER VALUE GREATER THAN OR EQUAL TO X.
2. DOUBLE FLOOR(DOUBLE X): THE C LIBRARY FUNCTION DOUBLE FLOOR(DOUBLE X)
RETURNS THE LARGEST INTEGER VALUE LESS THAN OR EQUAL TO X.
3. DOUBLE FABS(DOUBLE X): THE C LIBRARY FUNCTION DOUBLE FABS(DOUBLE X) RETURNS
THE ABSOLUTE VALUE OF X.
4. DOUBLE LOG(DOUBLE X): THE C LIBRARY FUNCTION DOUBLE LOG(DOUBLE X) RETURNS
THE NATURAL LOGARITHM (BASE-E LOGARITHM) OF X.
5. DOUBLE LOG10(DOUBLE X): THE C LIBRARY FUNCTION DOUBLE LOG10(DOUBLE X)
RETURNS THE COMMON LOGARITHM (BASE-10 LOGARITHM) OF X.
Some more function
6. DOUBLE FMOD(DOUBLE X, DOUBLE Y) : THE C LIBRARY FUNCTION DOUBLE
FMOD(DOUBLE X, DOUBLE Y) RETURNS THE REMAINDER OF X DIVIDED BY Y.
7. DOUBLE SQRT(DOUBLE X): THE C LIBRARY FUNCTION DOUBLE SQRT(DOUBLE X) RETURNS
THE SQUARE ROOT OF X.
8. DOUBLE POW(DOUBLE X, DOUBLE Y): THE C LIBRARY FUNCTION DOUBLE POW(DOUBLE X,
DOUBLE Y) RETURNS X RAISED TO THE POWER OF Y I.E. XY.
9. DOUBLE MODF(DOUBLE X, DOUBLE *INTEGER): THE C LIBRARY FUNCTION DOUBLE
MODF(DOUBLE X, DOUBLE *INTEGER) RETURNS THE FRACTION COMPONENT (PART AFTER
THE DECIMAL), AND SETS INTEGER TO THE INTEGER COMPONENT.
10. DOUBLE EXP(DOUBLE X): THE C LIBRARY FUNCTION DOUBLE EXP(DOUBLE X) RETURNS
THE VALUE OF E RAISED TO THE XTH POWER.
11. DOUBLE COS(DOUBLE X) : THE C LIBRARY FUNCTION DOUBLE COS(DOUBLE X) RETURNS
THE COSINE OF A RADIAN ANGLE X.
12. DOUBLE ACOS(DOUBLE X) : THE C LIBRARY FUNCTION DOUBLE ACOS(DOUBLE X)
RETURNS THE ARC COSINE OF X IN RADIANS.
13. DOUBLE TANH(DOUBLE X): THE C LIBRARY FUNCTION DOUBLE TANH(DOUBLE X)
RETURNS THE HYPERBOLIC TANGENT OF X.
5. String Manipulation in C++
A STRING IS A SEQUENCE OF CHARACTER. AS YOU KNOW THAT C++
DOES NOT SUPPORT BUILT-IN STRING TYPE, YOU HAVE USED EARLIER
THOSE NULL CHARACTER BASED TERMINATED ARRAY OF CHARACTERS
TO STORE AND MANIPULATE STRINGS. THESE STRINGS ARE TERMED
AS C STRINGS. IT OFTEN BECOMES INEFFICIENT PERFORMING
OPERATIONS ON C STRINGS. PROGRAMMERS CAN ALSO DEFINE THEIR
OWN STRING CLASSES WITH APPROPRIATE MEMBER FUNCTIONS TO
MANIPULATE STRINGS. ANSI STANDARD C++ INTRODUCES A NEW
CLASS CALLED STRING WHICH IS AN IMPROVISED VERSION OF C
STRINGS IN SEVERAL WAYS. IN MANY CASES, THE STRINGS OBJECT
MAY BE TREATED LIKE ANY OTHER BUILT-IN DATA TYPE. THE STRING IS
TREATED AS ANOTHER CONTAINER CLASS FOR C++.

THIS IS HOW THE STRINGS IN C ARE DECLARED:


• CHAR CH[6] = {'H', 'E', 'L', 'L', 'O', '\0'};
Manipulate Null-Terminated Strings
C++ SUPPORTS A WIDE RANGE OF FUNCTIONS THAT MANIPULATE
NULL-TERMINATED STRINGS. THESE ARE:
• STRCPY(STR1, STR2): COPIES STRING STR2 INTO STRING STR1.
• STRCAT(STR1, STR2): CONCATENATES STRING STR2 ONTO THE END
OF STRING STR1.
• STRLEN(STR1): RETURNS THE LENGTH OF STRING STR1.
• STRCMP(STR1, STR2): RETURNS 0 IF STR1 AND STR2 ARE THE SAME;
LESS THAN 0 IF STR1<STR2; GREATER THAN 0 IF STR1>STR2.
• STRCHR(STR1, CH): RETURNS A POINTER TO THE FIRST OCCURRENCE
OF CHARACTER CH IN STRING STR1.
• STRSTR(STR1, STR2): RETURNS A POINTER TO THE FIRST OCCURRENCE
OF STRING STR2 IN STRING STR1.
Important Functions Supported By String
Class
• APPEND(): THIS FUNCTION APPENDS A PART OF A STRING TO ANOTHER STRING
• ASSIGN():THIS FUNCTION ASSIGNS A PARTIAL STRING
• AT(): THIS FUNCTION OBTAINS THE CHARACTER STORED AT A SPECIFIED LOCATION
• BEGIN(): THIS FUNCTION RETURNS A REFERENCE TO THE START OF THE STRING
• CAPACITY(): THIS FUNCTION GIVES THE TOTAL ELEMENT THAT CAN BE STORED
• COMPARE(): THIS FUNCTION COMPARES A STRING AGAINST THE INVOKING STRING
• EMPTY(): THIS FUNCTION RETURNS TRUE IF THE STRING IS EMPTY
• END(): THIS FUNCTION RETURNS A REFERENCE TO THE END OF THE STRING
• ERASE(): THIS FUNCTION REMOVES CHARACTER AS SPECIFIED
• FIND(): THIS FUNCTION SEARCHES FOR THE OCCURRENCE OF A SPECIFIED SUBSTRING
• LENGTH(): IT GIVES THE SIZE OF A STRING OR THE NUMBER OF ELEMENTS OF A STRING
• SWAP(): THIS FUNCTION SWAPS THE GIVEN STRING WITH THE INVOKING ONE
6. Classes And Objects

IN OBJECT-ORIENTED PROGRAMMING LANGUAGES LIKE C++, THE


DATA AND FUNCTIONS (PROCEDURES TO MANIPULATE THE DATA) ARE
BUNDLED TOGETHER AS A SELF-CONTAINED UNIT CALLED AN OBJECT.
A CLASS IS AN EXTENDED CONCEPT SIMILAR TO THAT OF STRUCTURE IN
C PROGRAMMING LANGUAGE; THIS CLASS DESCRIBES THE DATA
PROPERTIES ALONE. IN C++ PROGRAMMING LANGUAGE, A CLASS
DESCRIBES BOTH THE PROPERTIES (DATA) AND BEHAVIORS
(FUNCTIONS) OF OBJECTS. CLASSES ARE NOT OBJECTS, BUT THEY ARE
USED TO INSTANTIATE OBJECTS.
What is a Class?
• A CLASS IS AN ABSTRACT DATA TYPE SIMILAR TO 'C STRUCTURE'.
• THE CLASS REPRESENTATION OF OBJECTS AND THE SETS OF OPERATIONS THAT CAN
BE APPLIED TO SUCH OBJECTS.
• THE CLASS CONSISTS OF DATA MEMBERS AND METHODS.
THE PRIMARY PURPOSE OF A CLASS IS TO HOLD DATA/INFORMATION. THIS IS
ACHIEVED WITH ATTRIBUTES WHICH ARE ALSO KNOWN AS DATA MEMBERS.
THE MEMBER FUNCTIONS DETERMINE THE BEHAVIOR OF THE CLASS I.E. PROVIDE A
DEFINITION FOR SUPPORTING VARIOUS OPERATIONS ON DATA HELD IN FORM OF AN
OBJECT.
• Private, Protected, Public is called Access Modifiers.
• The members that are declared private can be accessed only
from within the class.
• Public members can be accessed from outside the class also.
• In C++, data can be hidden by making it private.
Class Members
DATA AND FUNCTIONS ARE MEMBERS.
DATA MEMBERS AND METHODS MUST BE DECLARED WITHIN THE CLASS DEFINITION
• A MEMBER CANNOT BE RE-DECLARED WITHIN A CLASS.
• NO MEMBER CAN BE ADDED ELSEWHERE OTHER THAN IN THE CLASS DEFINITION.

NOTE: THE PUBLIC DATA MEMBERS OF OBJECTS OF A CLASS CAN BE ACCESSED USING
THE DIRECT MEMBER ACCESS OPERATOR (.).
7. Memory Management and pointers
DYNAMIC MEMORY ALLOCATION IN C/C++ REFERS TO PERFORMING
MEMORY ALLOCATION MANUALLY BY PROGRAMMER.
ONE USE OF DYNAMICALLY ALLOCATED MEMORY IS TO ALLOCATE MEMORY
OF VARIABLE SIZE WHICH IS NOT POSSIBLE WITH COMPILER ALLOCATED
MEMORY EXCEPT VARIABLE LENGTH ARRAYS.
C++ HAS TWO OPERATORS NEW AND DELETE THAT PERFORM THE TASK OF
ALLOCATING AND FREEING THE MEMORY IN A BETTER AND EASIER WAY.
New Operator
THE NEW OPERATOR DENOTES A REQUEST FOR MEMORY ALLOCATION
ON THE HEAP. IF SUFFICIENT MEMORY IS AVAILABLE, NEW OPERATOR
INITIALIZES THE MEMORY AND RETURNS THE ADDRESS OF THE NEWLY
ALLOCATED AND INITIALIZED MEMORY TO THE POINTER VARIABLE.
SYNTAX:

EXAMPLE:
Delete Operator
SINCE IT IS PROGRAMMER’S RESPONSIBILITY TO DEALLOCATE
DYNAMICALLY ALLOCATED MEMORY, PROGRAMMERS ARE
PROVIDED DELETE OPERATOR BY C++ LANGUAGE.
SYNTAX:

EXAMPLE:
8. Constructors in C++ :-
What is constructor?
A constructor is a member function of a class which initializes objects of
a class. In C++, Constructor is automatically called when
object(instance of class) create. It is special member function of the
class.

A constructor is different from normal functions in following ways:


•Constructor has same name as the class itself
•Constructors don’t have return type
•A constructor is automatically called when an object is created.
•If we do not specify a constructor, C++ compiler generates a default
constructor for us (expects no parameters and has an empty body)
Parametrized constructors :-
Parameterized Constructor: It may be necessary to initialize the various
data elements of different objects with different values when they are
created. This is achieved by passing arguments to the constructor function
when the objects are created
Multiple constructor in a class in C++
Overloaded constructors have the same name (name of the class) but different
number of arguments. Depending upon the number and type of arguments
passed, specific constructor is called. Since, there are multiple
constructors present, argument to the constructor should also be passed while
creating an object.
Destructors in C++
A destructor is a special member function that works just opposite to constructor,
unlike constructors that are used for initializing an object, destructors destroy (or
delete) the object. Similar to constructor, the destructor name should exactly
match with the class name.
9. Operator Overloading in C++
It is a type of polymorphism in which an operator is overloaded to give
user defined meaning to it. ... For example '+' operator can be overloaded to
perform addition on various data types, like for Integer, String(concatenation) etc.
Type conversion :-
10. Inheritance Extending Classes :-

When we say that members of a class are inheritable, it means that


the derived class can access them directly
Virtual Function

A VIRTUAL FUNCTION A MEMBER FUNCTION WHICH IS DECLARED


WITHIN A BASE CLASS AND IS RE-DEFINED(OVERRIDDEN) BY A DERIVED
CLASS. WHEN YOU REFER TO A DERIVED CLASS OBJECT USING A
POINTER OR A REFERENCE TO THE BASE CLASS, YOU CAN CALL A
VIRTUAL FUNCTION FOR THAT OBJECT AND EXECUTE THE DERIVED
CLASS’S VERSION OF THE FUNCTION.
• VIRTUAL FUNCTIONS ENSURE THAT THE CORRECT FUNCTION IS
CALLED FOR AN OBJECT, REGARDLESS OF THE TYPE OF REFERENCE
(OR POINTER) USED FOR FUNCTION CALL.
• THEY ARE MAINLY USED TO ACHIEVE RUNTIME POLYMORPHISM
• FUNCTIONS ARE DECLARED WITH A VIRTUAL KEYWORD IN BASE
CLASS.
• THE RESOLVING OF FUNCTION CALL IS DONE AT RUN-TIME.
Working with files in C++
13. Exception Handling :-

You might also like