Uploaded by Aman Choudhary

Adobe Scan May 25, 2023

advertisement
\
Content:
-----
-
1. Introduction
2. Classes and Objects
3. Relationships between dasses
4. Inheritance
5. Abstract class & Interface
6. Collections
7. Exception Handling
8. File Handling
9. String Handling
Introduction
What is C++?
C• • Is a c:ros"1)181fonn language lhal can be UMd 10 crellle ~ appllcatlons.
c... was de'veloped by Bjame SCrousuup, as an extension to ll'le C language.
c... gives pnigrwnmars •high....,.. of conlrol owr 1ystem rnourms and memory.
The laoguage-updalad 3majof bmel In 2011, 201, . and 2017 to Ctt 11, Ctt1', anc1c-1T.
WhyUMC++ 1
C ++ Is one of lhe wood's most popular programming languages.
C+-+ can be found in today's operating systems. Gtaphlcal Use, Interfaces. and embedded systems.
C++ is an object-orienled programming language which gives a clear structure lo programs and atlows
code to be reused, lowenng development costs.
C •+ is pottable and can be used to devefop aPt)hcations that can be adapted to multiple platforms.
C++ is fun and easy to leaml
As C+-+ is dose 10 Q! and ,lit2. it makM It easy for programmeni to switch 10 C++ or vtce versa
\
•
Applications of C++ Programming
As
ment,one<t belote, C •• is ono
almost ~
-
Ila!,_programmw,g
them,_.:
languagn. It hall ifa presonoo In
of Ille most widety used
of IOftwaf'9 CM.<etoc,ment. I'm p,g to
of
a1,,_,
Application Software Dewlopment . C• • programming l\aS been usea ,n develop,ng
al lhe
maJO( Openit,ng Syntffll ... Window&, Mac OSX and llnua. Apa,, from lhe 0C)Of11bng 1yatems. lhe COl1I
p4ll1 of many browsers lllte Mozilla Firwtox and Ctwome haYe been wnllen using C+-•. C .. also hall been
uaed 1n developing the ~ popular databne l"flllem cali.d MySOL
Programming
Lantuaen
Development •
program,nng languages••• CII, J■v■.
Computation Programming •
oomputa!Jonal efficae,non.
C•·•
C•• has
J■v■&:npt ,
been used eJrtensrvety In d4Mllcpng new
P9fl. UNIX's C Shel. PHP and Python. ■nd Venlog etc.
IS !he best lnendt of ICiefttlllt becauM of IHI sc,eed and
0 - . Development• C•• ta exltemely fut whdl allows programmera IIO do procedural programming
lot CPU in1 - lunct,on, and provides gnuter conlrol CMW h■n1ware. because of wto.ich it has been
widely used in de\Jelopmenl of gaming Ill lgi lft.
Embeddlld System • C++ 11 being hgwy used 1n dewlop<ng l',ledical and Eno--ring AppllClltiona like
softwares for MRI machines. high-end CADJCAM &)'Stems etc.
This 1111 ~ on. ther'e are vanous ~ ·~
de\Jelopera are happily using C++ to pn:,,vlde greal
softwares. I higNy recommend you to learn C+-+ amd rontnbute great softwa,ea to lhe communtty.
Classes and Objects
C++ CLASS
A ctass ,s a bluepnn1 10' the Object
We can u,1nk of a class es a sketch (prototype ) of a house.
/
II contains all the details about the flooo.. doors. windows, etc. Based on these descnptJons
we build the house
Actass is defined In C++ using keywo,d clua folo\'18d by !tie name of the dau.
The body of the class is defined Inside lhe w rly brackets and tennlnated by a sem1cok>n at
lhe end
class ~ssNnrr •
(
',Olllt! d,11;1
som<' func:t,on"
C++ OBJECTS
•
In C++, Object is a real wof1d entity, for example. chair. car. pen. mobile, laptop
etc.
•
Class is mere a blueprint or a template and no storage is assigned when we define
a class.
/
•
Objects are instances of class, which holds the data vanables declared In class
and the member functions worl( on these class objects.
•
Each object has different dala variable.
•
Object is a runtime entity, It is created at runtime.
•
To use the data and access funct.i ons defined in the class. we need to create
ob}ects.
Syntax to Define Object In C++
className obJectVariab!eName;
Relationships between classes
•
One of lhe advantages or Object-Oriented programming language Is code reuse.
This reusabilily is possible due lo the relationship b/w the classes.
All these relatK>nshlp Is based on "ls a· relationship. •~a· relationship and "parlor relationship.
Object oriented programming generally support 4 types of relationships lhal are:
1. Inheritance
2. Association.
3. Composition
4 . Aggregation.
Inheritance:
Inheritance is ·1S-A" type of relationship.
Inheritance is a parent-child relationship where we create· a new class by using existing ciass
code. It Is just hke saying that "A Is type of B". For example 1s "Apple Is a fruit". · Ferrari is a car".
Composition:
Composition is a "part-or' relationship.
In composition relationship both entities are interdependent of each other for example ·engine is
part of car". "heart 1s part of body·.
Association:
Association is a "has-a" type relationship . Association establish the relationship b/w two classes
using through their objects.
Assoclabon relationship can be one lo one. One to many. many to one and many to many.
For example suppose we have two classes then these two ciasses are said lo be "has-a·
relationship if both of these enti1ies share each other's o~ject for some work and at the same time
they can exists without each others dependency or both have their own life time.
•
Aggregation
Aggregation Is based Is on '"has-a· relationship. Aggregation is a special ronn of
association.
In association there is not any classes (entity) work as owner but in aggregation one
entity work as owner.
In aggregation both entities meet for some wor1< and then get separated. Aggregation
is a one way association.
Inheritance
The capab1hty of a class to derive properties and charactensltcs from another class
Is called Inheritance
Inheritance 1s one of the most Important feature of Ob1ect Oriented Programming.
•
Sub Class: The class that inherits properties from another class ,s called Sub
class or Derived Class.
Super Class: The class whose properties are inherited by sub clas.s is called Base
Class or Super class.
• We group the "inheritance concept" Into two categories:
derived class (child) - the class that inherits from another class
base class (parent) - the class being inherited from
Modes of Inheritance
1. Public mode: If we denve a sub class from a public base class. Then the pubhc member
of the base class will become public in lhe derived class and protected members of lhe
base class will become protected in derived class.
2. Protected mode: If we derive a sub class from a Protected base class. Then both public
member and protected members of the base class will become protected In derived
class.
3. Private mode: If we derive a sub class from a Private base class. Then both pubflc
member and protected members of the base dass will become Private in derived dass.
Access Control and lnhentance
public
Access
protected
private
Same class
yes
yes
yes
Derived classes
yes
yes
no
Outside classes
yes
no
no
Abstract class & Interface
Abstract class
An abstract class in C++ Is a class that has at least one pure virtual function
or abstract function (i.e .. a function that has no definition).
Abstract classes are essential to providing an abstraction to the code to make it
reusable and extendable.
Abstract classes are used to provide an Interface for its sub classes.
Pure Virtual Functions In C++
Pure virtual Functions .are virtual functions with no definition.
They start with virtual keyword and ends with
Here rs the syntax for a pure virtual function,
void _f () ;:: 0 ;
Characteristics of Abstract Class
1. Abstract class cannot be instantiated. but pointers and refrences of Abstract dass
type can be created.
2. Abstract dass can have normal functions and variables along with a pure virtual
function.
3. Abstract classes are mainly used for Upcasting. :so that its derived classes can use
its interface.
4. Classes inheriting an Abstract Class must implement all pure virtual functions, or
else they will become Abstract too.
Collections
•
A collection class is a container clas.s that holds one or more items of a certain type.
•
A collection class Is characterized by Its "shape" and by the types of its elements.
The shape refers to the way the objects are organized and stored by the collection.
•
MFC provides three basic collection shapes: lists, arrays, and maps (also known as
dictionaries)
Ust
The list class provides an ordered, nonindexed list of elements, implemented as a doubly linked
list. A list has a "head" and a "tail." and adding or removing elements from the head or tall, or
inserting or deleting elements in the m iddle, is very fast.
Array
The array class provides a dynamically sized, ordered, and integer-indexed array of objects.
Map (also known as a dictionary)
A map is a collection that associates a key object with a value object.
Exception Handling
•
One of the advantages of C++ over C Is Exception Handling.
• An exception is a problem that arises during the execution of a program.
•
Provide a way to transfer control from one part of a program to another.
•
There are two types of exceptions:
a)Synchronous
b )Asynchronous
(Eg: which are beyond the program's control. Disc failure etc.)
/
•
C++ exception handling is built upon three keywords:
·
throw - It is used to throw exceptions to exception handler i.e . It is used to
communicate Information about error. This is done using a throw keyword.
• catch - A program catches an exception with an exception handler at the
place In a program where you want to handle
The catch keyword Indicates the catching of an exception.
the
problem.
• try - A try block Identifies a block of code for which particular exceptions
will be activated. It's followed by one or more catch blocks.
File Handling
• In Flies we store data i.e. text or binary data permanently and use these data to
read or write In the form of Input output operations by transferring bytes of data.
•
We use the header file • ts t reum>
Data types used for file han,dllng from the fstream library:
•
ofatream: It represents output Stream and this is used for writing in flies.
•
lfstream: It represents Input Stream and this Is used for reading from flies.
•
fatream: It represents both output Stream and input Stream. So it can read from files
and write to files.
Operations In File Handling:
•Creating a file: r .We create/open a file by specifying new path of the file and mode of operation.
Operations can be reading, writing, appending and truncating.
•
·I
Syntax for file creation: r ~.Lct-u.01..• :. • upl!r I '' Po:.h ", .us : : ~ode I ;
•Reading data: t• J' ' I)
We use << and >> to read from a file respectively
•Writing new data:
11
We use << and >> to write from a file respectively
•Closing a file: c
1
•
C++ automatically close and release al the allocated memory. But a programmer should always
close all the opened files
String Handling
•
In C++. one can make string handling lot simpler using strings and string steam
class.
In C++ we can store stnng by one of lhe two ways -
.I
1. C style strings
2. string class
Strings are used for storing text.
A string variable contains a collection of ciharacters surrounded by d,ouble
quotes:
Eg: string gr-Hting • "He llo" ;
•
Following are some of the C•+ String functions we can use:
•
Subatr(beginning char index, from that index how many characters you want.)
As the name suggests it take the substring from the given string.
Strcat(atr1,atr2): Appending the string
This string function in C++ combines two different strings.
•
Strcmp(atr1 ,atr2): Returns -ve value if str1 Is less than str2;0 if str1 Is equal to str2; • ,d >O
(+ve value) if str1 Is greater than str2.
This string function in C++ combines two different strings.
Strcpy(str1,str2): Replace the content
Unlike strcat, ll will not append string into other and it will replace all the content,
Strlen(str1): Gives the length of the string
The simplest function in a row.
This function defined in <cstring> header file. This function returns the length of the string.
Download