[go: up one dir, main page]

0% found this document useful (0 votes)
3 views22 pages

c++ (5)

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 22

What Is C++ Programming Language?

C++ is a versatile and powerful programming


language that is an extension of basic C
programming. It introduced the object oriented
programming concepts, which helped developers
create modular and reusable code with classes and
objects. It is a popular choice for system software,
game development, and other critical operations. It
contains a lot of special features, such as Standard
Template Library (STL), which helps in implementing
various operations.
Features
1.Simple
2.Abstract Data types
3.Machine Independent or Portable
4.Mid-level programming language
5.Structured programming language
6.Rich Library
7.Memory Management
8.Quicker Compilation
9.Pointers
10.Recursion
11.Extensible
12.Object-Oriented
13.Compiler based
14.Reusability
15.National Standards
16.Errors are easily detected
17.Power and Flexibility
18.Strongly typed language
19.Redefine Existing Operators
20.Modeling Real-World Problems
21.Clarity
Benefits of OOP
•We can build the programs from standard working modules that communicate with one another,
rather than having to start writing the code from scratch which leads to saving of development time
and higher productivity,
•OOP language allows to break the program into the bit-sized problems that can be solved easily (one
object at a time).
•The new technology promises greater programmer productivity, better quality of software and lesser
maintenance cost.
•OOP systems can be easily upgraded from small to large systems.
•It is possible that multiple instances of objects co-exist without any interference,
•It is very easy to partition the work in a project based on objects.
•It is possible to map the objects in problem domain to those in the program.
•The principle of data hiding helps the programmer to build secure programs which cannot be
invaded by the code in other parts of the program.
•By using inheritance, we can eliminate redundant code and extend the use of existing classes.
•Message passing techniques is used for communication between objects which makes the interface
descriptions with external systems much simpler.
•The data-centered design approach enables us to capture more details of model in an
implementable form.
Object-Oriented Programming Terms
1. Objects
Objects are the basic units of code used to create software applications in OOP languages. All OOP
languages use objects to build programs because they're easy to replicate and scale. An object can be any
abstract data type that has specific characteristics and functions. For example, a specific user account in
an online database is an object. This object has its own unique characteristics and can perform certain
functions, like browsing or editing articles.
2. Classes
Classes are the parameters that help a program create objects. While all OOP coding
languages support objects, not all of them have classes. Many coding languages support
both objects and classes. Each class has its own set of qualifications, and individual
objects have unique data for each qualification.
3. Inheritance
If an OOP language supports classes, then it probably also uses the concept of
inheritance to organize classes. Inheritance allows programmers to relate certain
classes to one another, providing structure within the program. In this structure, some
classes are "parents," while others are "children."
Encapsulation
Programmers use encapsulation to protect certain information within a class from
the rest of the code. They can hide specific attributes within the object and restrict
access to the attribute to members of a certain class or subclass. For example, a user
account on a website might include sensitive information, like social security
numbers or home addresses. These attributes can be encapsulated so that only
certain classes can access the information.

Polymorphism
Polymorphism relates to the ability of different subclasses in a hierarchy to respond to
the same command in their own ways. A hierarchy is a set of classes that relate to each
other under the principle of inheritance. For example, you might have a superclass
called "Shapes," with subclasses for "Circles" and "Squares."
Defining Class in C++
A class is defined in C++ using the keyword class followed by the name of the class. The following
is the syntax:
class ClassName {
access_specifier:
// Body of the class
};

Example
class ThisClass {
public:
int var; // data member
void print() { // member
method
cout << "Hello";
}
};
What is an Object in C++?
When a class is defined, only the specification for the object is defined; no
memory or storage is allocated. To use the data and access functions defined in
the class, you need to create objects.
Syntax to Create an Object
We can create an object of the given class in the same way we declare the
variables of any other inbuilt data type.
ClassName ObjectName;
Example
MyClass obj;
Class Methods
Methods are functions that belongs to the class.
There are two ways to define functions that belongs
to a class:
•Inside class definition
•Outside class definition
No. Class Structure
Members of a class are private by Members of a structure are public
1 default. by default.
It is declared using It is declared using
2 the class keyword. the struct keyword.
It is normally used for data It is normally used for the
3 abstraction and inheritance. grouping of different datatypes.

Syntax:
Syntax:
class class_name {
struct structure_name {
4 data_member;
structure_member1;
member_function;
structure_member2;
};
A nested class is a class which is declared in another enclosing class. A nested
class is a member and as such has the same access rights as any other
member. The members of an enclosing class have no special access to
members of a nested class; the usual access rules shall be obeyed.
Constructor in C++ is a special method that is invoked
automatically at the time an object of a class is created. It is
used to initialize the data members of new objects generally.
The constructor in C++ has the same name as the class or
structure. It constructs the values i.e. provides data for the
object which is why it is known as a constructor.
Syntax of Constructors in C++
The prototype of the constructor looks like this:
<class-name> (){
...
}
Destructor is an instance member function that is invoked
automatically whenever an object is going to be destroyed. Meaning,
a destructor is the last function that is going to be called before an
object is destroyed.
Syntax to Define Destructor
The syntax for defining the destructor within the class:
~ <class-name>() {
// some instructions
}
Static data members are class members that are declared
using static keywords.
Syntax
className {
static data_type data_member_name;
.....
}
Static data members are useful for maintaining data
shared among all instances of a class.
The capability of a class to derive properties and characteristics from another
class is called Inheritance. Inheritance is one of the most important features of
Object Oriented Programming in C++. In this article, we will learn about
inheritance in C++, its modes and types along with the information about how it
affects different properties of the class.
Syntax of Inheritance in C++
class derived_class_name : access-specifier base_class_name
{
// body ....
};
where,
•class: keyword to create a new class
•derived_class_name: name of the new class, which will inherit the base class
•access-specifier: Specifies the access mode which can be either of private, public or protected. If neither is specified,
private is taken as default.
•base-class-name: name of the base class.
Types Of Inheritance in C++
The inheritance can be classified on the basis of the relationship between the derived class
and the base class. In C++, we have 5 types of inheritances:
1.Single inheritance
2.Multilevel inheritance
3.Multiple inheritance
4.Hierarchical inheritance
5.Hybrid inheritance
1. Single Inheritance
In single inheritance, a class is allowed to inherit from only one class. i.e. one base class is
inherited by one derived class only.
Multiple Inheritance
Multiple Inheritance is a feature of C++ where a class can inherit from more than one class. i.e one subclass is
inherited from more than one base class.
Multilevel Inheritance
In this type of inheritance, a derived class is created from another derived class and that derived class can be
derived from a base class or any other derived class. There can be any number of levels.
Hierarchical Inheritance
In this type of inheritance, more than one subclass is inherited from a single base class. i.e. more than one
derived class is created from a single base class.
Hybrid Inheritance
Hybrid Inheritance is implemented by combining more than one type of inheritance. For example: Combining
Hierarchical inheritance and Multiple Inheritance will create hybrid inheritance in C++
A virtual function (also known as virtual methods) is a
member function that 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 method.
A friend class can access private and protected members of other classes in
which it is declared as a friend. It is sometimes useful to allow a particular
class to access private and protected members of other classes. For example,
a LinkedList class may be allowed to access private members of Node.
We can declare a friend class in C++ by using the friend keyword.
Syntax:
friend class class_name; // declared in the base class
in C++, Operator overloading is a compile-time polymorphism. It is
an idea of giving special meaning to an existing operator in C++
without changing its original meaning.
C++ has the ability to provide the operators with a special meaning
for a data type, this ability is known as operator overloading.
Operator overloading is a compile-time polymorphism. For example,
we can overload an operator ‘+’ in a class like String so that we can
concatenate two strings by just using +. Other example classes
where arithmetic operators may be overloaded are Complex
Numbers, Fractional Numbers, Big integers, etc.
Type conversion is essential for managing different data types in C++. The C++
Course covers the various methods of type conversion, helping you
understand how to handle data types correctly. A type cast is basically a
conversion from one type to another. There are two types of type conversion:
1.Implicit Type Conversion Also known as ‘automatic type conversion’.

Explicit Type Conversion: This process is also called type casting and
it is user-defined.
Data Type Description

It is used to create files, write


fstream information to files, and read
information from files.

It is used to read information


ifstream
from files.

It is used to create files and


ofstream
write information to the files.
File handling in C++ is a mechanism to create and perform read/write operations
on a file.
We can access various file handling methods in C++ by importing
the <fstream> class.
#include <fstream>
<fstream> includes two classes for file handling:
•ifstream - to read from a file.
•ofstream - to create/open and write to a file.

You might also like