Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
SlideShare a Scribd company logo
Summer Internship MOOC’s
Name: KAUSHAL KUMAR JHA
Registration number:11802169
Course code: CSE443
MOOC’s course name: OBJECT ORIENTED PROGRAMMING USING C++
Content:
 1. Introduction
 2. Classes and Objects
 3. Relationships between classes
 4. Inheritance
 5. Abstract class & Interface
 6. Collections
 7. Exception Handling
 8. File Handling
 9. String Handling
Introduction
What is C++?
C++ is a cross-platform language that can be used to create high-performance applications.
C++ was developed by Bjarne Stroustrup, as an extension to the C language.
C++ gives programmers a high level of control over system resources and memory.
The language was updated 3 major times in 2011, 2014, and 2017 to C++11, C++14, and C++17.
Why Use C++ ?
C++ is one of the world's most popular programming languages.
C++ can be found in today's operating systems, Graphical User Interfaces, and embedded systems.
C++ is an object-oriented programming language which gives a clear structure to programs and allows
code to be reused, lowering development costs.
C++ is portable and can be used to develop applications that can be adapted to multiple platforms.
C++ is fun and easy to learn!
As C++ is close to C# and Java, it makes it easy for programmers to switch to C++ or vice versa
.
 Applications of C++ Programming
As mentioned before, C++ is one of the most widely used programming languages. It has it's presence in
almost every area of software development. I'm going to list few of them here:
• Application Software Development - C++ programming has been used in developing almost all the
major Operating Systems like Windows, Mac OSX and Linux. Apart from the operating systems, the core
part of many browsers like Mozilla Firefox and Chrome have been written using C++. C++ also has been
used in developing the most popular database system called MySQL.
• Programming Languages Development - C++ has been used extensively in developing new
programming languages like C#, Java, JavaScript, Perl, UNIX’s C Shell, PHP and Python, and Verilog etc.
• Computation Programming - C++ is the best friends of scientists because of fast speed and
computational efficiencies.
• Games Development - C++ is extremely fast which allows programmers to do procedural programming
for CPU intensive functions and provides greater control over hardware, because of which it has been
widely used in development of gaming engines.
• Embedded System - C++ is being heavily used in developing Medical and Engineering Applications like
softwares for MRI machines, high-end CAD/CAM systems etc.
This list goes on, there are various areas where software developers are happily using C++ to provide great
softwares. I highly recommend you to learn C++ and contribute great softwares to the community.
Classes and Objects
C++ CLASS
• A class is a blueprint for the object.
• We can think of a class as a sketch (prototype) of a house.
• It contains all the details about the floors, doors, windows, etc. Based on these descriptions
we build the house.
• A class is defined in C++ using keyword class followed by the name of the class.
• The body of the class is defined inside the curly brackets and terminated by a semicolon at
the end.
class className
{
// some data
// some functions
.
C++ OBJECTS
• In C++, Object is a real world 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 variables declared in class
and the member functions work on these class objects.
• Each object has different data variable.
• Object is a runtime entity, it is created at runtime.
• To use the data and access functions defined in the class, we need to create
objects.
Syntax to Define Object in C++
className objectVariableName;
Relationships between classes
• One of the advantages of Object-Oriented programming language is code reuse.
This reusability is possible due to the relationship b/w the classes.
• All these relationship is based on "is a" relationship, "has-a" relationship and "part-
of" relationship.
• Object oriented programming generally support 4 types of relationships that are:
1. Inheritance
2. Association,
3. Composition
4. Aggregation.
.
Inheritance:
Inheritance is “IS-A” type of relationship.
Inheritance is a parent-child relationship where we create a new class by using existing class
code. It is just like saying that “A is type of B”. For example is “Apple is a fruit”, “Ferrari is a car”.
Composition:
Composition is a "part-of" relationship.
In composition relationship both entities are interdependent of each other for example “engine is
part of car”, “heart is part of body”.
Association:
Association is a “has-a” type relationship. Association establish the relationship b/w two classes
using through their objects.
Association relationship can be one to one, One to many, many to one and many to many.
For example suppose we have two classes then these two classes are said to be “has-a”
relationship if both of these entities share each other’s object 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 form 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 work and then get separated. Aggregation
is a one way association.
Inheritance
• The capability of a class to derive properties and characteristics from another class
is called Inheritance
• Inheritance is one of the most important feature of Object Oriented Programming.
• Sub Class: The class that inherits properties from another class is called Sub
class or Derived Class.
• Super Class: The class whose properties are inherited by sub class 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 derive a sub class from a public base class. Then the public member
of the base class will become public in the derived class and protected members of the
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 public
member and protected members of the base class will become Private in derived class.
Access Control and Inheritance
Access public 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 = 0.
Here is the syntax for a pure virtual function,
Virtu void f() = 0;
.
Characteristics of Abstract Class
1. Abstract class cannot be instantiated, but pointers and refrences of Abstract class
type can be created.
2. Abstract class 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 class 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)
List
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 tail, or
inserting or deleting elements in the middle, 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 problem.
The catch keyword indicates the catching of an exception.
• 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 Files 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 <fstream>
Data types used for file handling from the fstream library:
• ofstream: It represents output Stream and this is used for writing in files.
• ifstream: It represents input Stream and this is used for reading from files.
• fstream: 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: open()
We create/open a file by specifying new path of the file and mode of operation.
Operations can be reading, writing, appending and truncating.
• Syntax for file creation: FilePointer.open("Path",ios::mode);
•Reading data: read()
We use << and >> to read from a file respectively
•Writing new data: write()
We use << and >> to write from a file respectively
•Closing a file: close()
C++ automatically close and release all 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 string by one of the two ways –
1. C style strings
2. string class
Strings are used for storing text.
A string variable contains a collection of characters surrounded by double
quotes:
Eg: string greeting = "Hello";
.
 Following are some of the C++ String functions we can use:
• Substr(beginning char index, from that index how many characters you want.)
As the name suggests it take the substring from the given string.
• Strcat(str1,str2): Appending the string
This string function in C++ combines two different strings.
• Strcmp(str1,str2): Returns -ve value if str1 is less than str2;0 if str1 is equal to str2; and >0
(+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, it 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.
Thank You

More Related Content

Summer Training Project On C++

  • 1. Summer Internship MOOC’s Name: KAUSHAL KUMAR JHA Registration number:11802169 Course code: CSE443 MOOC’s course name: OBJECT ORIENTED PROGRAMMING USING C++
  • 2. Content:  1. Introduction  2. Classes and Objects  3. Relationships between classes  4. Inheritance  5. Abstract class & Interface  6. Collections  7. Exception Handling  8. File Handling  9. String Handling
  • 3. Introduction What is C++? C++ is a cross-platform language that can be used to create high-performance applications. C++ was developed by Bjarne Stroustrup, as an extension to the C language. C++ gives programmers a high level of control over system resources and memory. The language was updated 3 major times in 2011, 2014, and 2017 to C++11, C++14, and C++17. Why Use C++ ? C++ is one of the world's most popular programming languages. C++ can be found in today's operating systems, Graphical User Interfaces, and embedded systems. C++ is an object-oriented programming language which gives a clear structure to programs and allows code to be reused, lowering development costs. C++ is portable and can be used to develop applications that can be adapted to multiple platforms. C++ is fun and easy to learn! As C++ is close to C# and Java, it makes it easy for programmers to switch to C++ or vice versa
  • 4. .  Applications of C++ Programming As mentioned before, C++ is one of the most widely used programming languages. It has it's presence in almost every area of software development. I'm going to list few of them here: • Application Software Development - C++ programming has been used in developing almost all the major Operating Systems like Windows, Mac OSX and Linux. Apart from the operating systems, the core part of many browsers like Mozilla Firefox and Chrome have been written using C++. C++ also has been used in developing the most popular database system called MySQL. • Programming Languages Development - C++ has been used extensively in developing new programming languages like C#, Java, JavaScript, Perl, UNIX’s C Shell, PHP and Python, and Verilog etc. • Computation Programming - C++ is the best friends of scientists because of fast speed and computational efficiencies. • Games Development - C++ is extremely fast which allows programmers to do procedural programming for CPU intensive functions and provides greater control over hardware, because of which it has been widely used in development of gaming engines. • Embedded System - C++ is being heavily used in developing Medical and Engineering Applications like softwares for MRI machines, high-end CAD/CAM systems etc. This list goes on, there are various areas where software developers are happily using C++ to provide great softwares. I highly recommend you to learn C++ and contribute great softwares to the community.
  • 5. Classes and Objects C++ CLASS • A class is a blueprint for the object. • We can think of a class as a sketch (prototype) of a house. • It contains all the details about the floors, doors, windows, etc. Based on these descriptions we build the house. • A class is defined in C++ using keyword class followed by the name of the class. • The body of the class is defined inside the curly brackets and terminated by a semicolon at the end. class className { // some data // some functions
  • 6. . C++ OBJECTS • In C++, Object is a real world 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 variables declared in class and the member functions work on these class objects. • Each object has different data variable. • Object is a runtime entity, it is created at runtime. • To use the data and access functions defined in the class, we need to create objects. Syntax to Define Object in C++ className objectVariableName;
  • 7. Relationships between classes • One of the advantages of Object-Oriented programming language is code reuse. This reusability is possible due to the relationship b/w the classes. • All these relationship is based on "is a" relationship, "has-a" relationship and "part- of" relationship. • Object oriented programming generally support 4 types of relationships that are: 1. Inheritance 2. Association, 3. Composition 4. Aggregation.
  • 8. . Inheritance: Inheritance is “IS-A” type of relationship. Inheritance is a parent-child relationship where we create a new class by using existing class code. It is just like saying that “A is type of B”. For example is “Apple is a fruit”, “Ferrari is a car”. Composition: Composition is a "part-of" relationship. In composition relationship both entities are interdependent of each other for example “engine is part of car”, “heart is part of body”. Association: Association is a “has-a” type relationship. Association establish the relationship b/w two classes using through their objects. Association relationship can be one to one, One to many, many to one and many to many. For example suppose we have two classes then these two classes are said to be “has-a” relationship if both of these entities share each other’s object for some work and at the same time they can exists without each others dependency or both have their own life time.
  • 9. . • Aggregation Aggregation is based is on "has-a" relationship. Aggregation is a special form 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 work and then get separated. Aggregation is a one way association.
  • 10. Inheritance • The capability of a class to derive properties and characteristics from another class is called Inheritance • Inheritance is one of the most important feature of Object Oriented Programming. • Sub Class: The class that inherits properties from another class is called Sub class or Derived Class. • Super Class: The class whose properties are inherited by sub class 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
  • 11. . Modes of Inheritance 1. Public mode: If we derive a sub class from a public base class. Then the public member of the base class will become public in the derived class and protected members of the 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 public member and protected members of the base class will become Private in derived class. Access Control and Inheritance Access public protected private Same class yes yes yes Derived classes yes yes no Outside classes yes no no
  • 12. 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 = 0. Here is the syntax for a pure virtual function, Virtu void f() = 0;
  • 13. . Characteristics of Abstract Class 1. Abstract class cannot be instantiated, but pointers and refrences of Abstract class type can be created. 2. Abstract class 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.
  • 14. Collections • A collection class is a container class 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) List 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 tail, or inserting or deleting elements in the middle, 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.
  • 15. 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.)
  • 16. . • 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 problem. The catch keyword indicates the catching of an exception. • 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.
  • 17. File Handling • In Files 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 <fstream> Data types used for file handling from the fstream library: • ofstream: It represents output Stream and this is used for writing in files. • ifstream: It represents input Stream and this is used for reading from files. • fstream: It represents both output Stream and input Stream. So it can read from files and write to files.
  • 18. . Operations in File Handling: •Creating a file: open() We create/open a file by specifying new path of the file and mode of operation. Operations can be reading, writing, appending and truncating. • Syntax for file creation: FilePointer.open("Path",ios::mode); •Reading data: read() We use << and >> to read from a file respectively •Writing new data: write() We use << and >> to write from a file respectively •Closing a file: close() C++ automatically close and release all the allocated memory. But a programmer should always close all the opened files
  • 19. String Handling • In C++, one can make string handling lot simpler using strings and string steam class. In C++ we can store string by one of the two ways – 1. C style strings 2. string class Strings are used for storing text. A string variable contains a collection of characters surrounded by double quotes: Eg: string greeting = "Hello";
  • 20. .  Following are some of the C++ String functions we can use: • Substr(beginning char index, from that index how many characters you want.) As the name suggests it take the substring from the given string. • Strcat(str1,str2): Appending the string This string function in C++ combines two different strings. • Strcmp(str1,str2): Returns -ve value if str1 is less than str2;0 if str1 is equal to str2; and >0 (+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, it 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.