0% found this document useful (0 votes)
11 views

Object Oriented Programming (1) (2)

The document outlines the syllabus and concepts of Object-Oriented Programming (OOP), including fundamental principles such as classes, objects, inheritance, encapsulation, and polymorphism. It discusses advantages and disadvantages of OOP compared to procedural programming, as well as practical examples in C++. Additionally, it covers advanced topics like exception handling, templates, and object composition.

Uploaded by

tarun.ydv.8000
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Object Oriented Programming (1) (2)

The document outlines the syllabus and concepts of Object-Oriented Programming (OOP), including fundamental principles such as classes, objects, inheritance, encapsulation, and polymorphism. It discusses advantages and disadvantages of OOP compared to procedural programming, as well as practical examples in C++. Additionally, it covers advanced topics like exception handling, templates, and object composition.

Uploaded by

tarun.ydv.8000
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 57

Object Oriented Programming

Syllabus
• Unit - I Object-Oriented Programming Concepts: Introduction, comparison between procedural
programming paradigm and object-oriented programming paradigm, basic concepts of object oriented
programming — concepts of an object and a class, interface and implementation of a class, operations on
objects, relationship among objects, abstraction, encapsulation, data hiding, inheritance, overloading,
polymorphism, messaging. Classes and Objects: Specifying a class, creating class objects, accessing
class members, access specifiers, static members, use of const keyword, friends of a class, empty classes,
nested classes, local classes, abstract classes, container classes, bit fields and classes.

• Unit – II
• Inheritance: Introduction, defining derived classes, forms of inheritance, ambiguity in multiple and
multipath inheritance, virtual base class, object slicing, overriding member functions, object composition
and delegation, order of execution of constructors and destructors. Pointers and Dynamic Memory
Management: Declaring and initializing pointers, accessing data through pointers, pointer arithmetic,
memory allocation (static and dynamic), dynamic memory management using new and delete operators,
pointer to an object, this pointer, pointer related problems - dangling/wild pointers, null pointer
assignment, memory leak and allocation failures.
• Unit - III Constructors and Destructors: Need for constructors and destructors,
copy constructor, dynamic constructors, explicit constructors, destructors,
constructors and destructors with static members, initializer lists. Operator
Overloading and Type Conversion: Overloading operators, rules for overloading
operators, overloading of various operators, type conversion - basic type to class
type, class type to basic type, class type to another class type. Virtual functions &
Polymorphism: Concept of binding - early binding and late binding, virtual
functions, pure virtual functions, abstract clasess, virtual destructors

• Unit - IV Exception Handling: Review of traditional error handling, basics of


exception handling, exception handling mechanism, throwing mechanism,
catching mechanism, rethrowing an exception, specifying exceptions. Templates
and Generic Programming: Template concepts, Function templates, class
templates, illustrative examples.
Concept of Object Oriented Programming

OOP is an approach or a Programming pattern where the programs are


structured around objects rather than functions and logic. It makes the data
partitioned into two memory areas, i.e., data and functions, and helps make
the code flexible and modular.

Object-oriented programming mainly focuses on objects that are required to


be manipulated. In OOPs, it can represent data as objects that have attributes
and functions.
Basic Object-Oriented Programming
 Object-An O Object can be defined as an entity that has a state and
behavior, or in other words, anything that exists physically in the world
is called an object. It can represent a dog, a person, a table, etc. An object
means the combination of data and programs, which further represent an
entity.
 Classes-Class can be defined as a blueprint of the object. It is basically a
collection of objects which act as building blocks.
 Abstraction- Abstraction helps in the data hiding process. It helps in
displaying the essential features without showing the details or the
functionality to the user. It avoids unnecessary information or irrelevant
details and shows only that specific part which the user wants to see.
 Encapsulation- The wrapping up of data and functions together in a single unit is
known as encapsulation. It can be achieved by making the data members' scope
private and the member function’s scope public to access these data members.
Encapsulation makes the data non-accessible to the outside world.
 Inheritance- Inheritance is the process in which two classes have an is-a
relationship among each other and objects of one class acquire properties and
features of the other class. The class which inherits the features is known as the
child class, and the class whose features it inherited is called the parent class. For
example, Class Vehicle is the parent class, and Class Bus, Car, and Bike are child
classes.
 Polymorphism- It means many forms. It is the ability to take more than one form.
It is a feature that provides a function or an operator with more than one definition.
It can be implemented using function overloading, operator overload, function
overriding, virtual function.
Advantages of OOPs
There are various advantages of object-oriented programming.
 OOPs provide reusability to the code and extend the use of existing classes.
 In OOPs, it is easy to maintain code as there are classes and objects, which helps
in making it easy to maintain rather than restructuring.
 It also helps in data hiding, keeping the data and information safe from leaking or
getting exposed.
 Object-oriented programming is easy to implement.
 OOP language allows to break the program into the bit-sized problems that can be
solved easily (one object at a time).
 Message passing techniques is used for communication between objects which
makes the interface descriptions with external systems much simpler.
Disadvantages of OOP
• The length of the programmes developed using OOP language is much
larger than the procedural approach. Since the programme becomes
larger in size, it requires more time to be executed that leads to slower
execution of the programme.
• We can not apply OOP everywhere as it is not a universal language. It
is applied only when it is required. It is not suitable for all types of
problems.
• Programmers need to have brilliant designing skill and programming
skill along with proper planning because using OOP is little bit tricky.
• OOPs take time to get used to it. The thought process involved in
object-oriented programming may not be natural for some people.
Procedural versus Object-Oriented
Programming
• In procedural programming, the program is divided into small parts
called functions. In object-oriented programming, the program is
divided into small parts called objects.
• Procedural programming follows a top-down approach. Object-
oriented programming follows a bottom-up approach.
• There is no access specifier in procedural programming. Object-
oriented programming has access specifiers like private, public,
protected, etc.
• Adding new data and functions is not easy. Adding new data and
function is easy.
Procedural versus Object-Oriented
Programming
• Procedural programming does not have any proper way of hiding data so
it is less secure. Object-oriented programming provides data hiding so it
is more secure.
• In procedural programming, overloading is not possible. Overloading is
possible in object-oriented programming.
• In procedural programming, there is no concept of data hiding and
inheritance. In object-oriented programming, the concept of data hiding
and inheritance is used.
• In procedural programming, the function is more important than the
data. In object-oriented programming, data is more important than
function.
Procedural versus Object-Oriented
Programming
• Procedural programming is based on the unreal world. Object-
oriented programming is based on the real world.
• Procedural programming is used for designing medium-sized
programs. Object-oriented programming is used for designing large
and complex programs.
• Procedural programming uses the concept of procedure abstraction.
Object-oriented programming uses the concept of data abstraction.
• Code reusability absent in procedural programming. Code reusability
present in object-oriented programming.
Writing First C++ Program – Hello World Example

// C++ program to display "Hello World"

// Header file for input output functions


#include <iostream>
using namespace std;

// Main() function: where the execution of


// program begins
int main()
{
// Prints hello world
cout << "Hello World";

return 0;
}
• #include
This is a preprocessor directive. The #include directive tells the compiler to include
the content of a file in the source code. For example, #include<iostream> tells the
compiler to include the standard iostream file which contains declarations of all the
standard input/output library functions.

• using namespace std


This is used to import the entity of the std namespace into the current namespace of the
program.

• int main() { }
A function is a group of statements that are designed to perform a specific task. The
main() function is the entry point of every C++ program, no matter where the function
is located in the program.
The opening braces ‘{‘ indicates the beginning of the main function and the closing
braces ‘}’ indicates the ending of the main function.
• cout<<“Hello World”;
std::cout is an instance of the std::ostream class, that is used to display
output on the screen. Everything followed by the character << in double
quotes ” ” is displayed on the output device. The semi-colon character
at the end of the statement is used to indicate that the statement is
ending there.

• return 0
This statement is used to return a value from a function and indicates the
finishing of a function. This statement is basically used in functions to
return the results of the operations performed by a function.
C++ Classes and Objects
• A class 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 C++ class is like a blueprint for an object.
• A Class is a user-defined data type that has data members and member
functions.
• Data members are the data variables and member functions are the
functions used to manipulate these variables together, these data
members and member functions define the properties and behaviour of
the objects in a Class.
• Note: When a class is defined, no memory is allocated but when it is
instantiated (i.e. an object is created) memory is allocated.
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
};
Here, the access specifier defines the level of access to the class’s data
members.
class ThisClass {
public:
int var; // data member
void print() { // member method
cout << "Hello";
}
};
• 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;
In the above statement, the object of MyClass with name obj is created.
• Accessing Data Members and Member Functions
The data members and member functions of the class can be accessed using
the dot(‘.’) operator with the object. For example, if the name of the object
is obj and you want to access the member function with the
name printName() then you will have to write:
obj.printName()
• 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;
In the above statement, the object of MyClass with name obj is created.
• Accessing Data Members and Member Functions
The data members and member functions of the class can be accessed using the
dot(‘.’) operator with the object. For example, if the name of the object
is obj and you want to access the member function with the
name printName() then you will have to write:
obj.printName()
Example of Class and Object in C++
#include <iostream>
#include <string>
using namespace std;
// Define a class named 'Person'
class Person {
public:
// Data members
string name;
int age;
// Member function to introduce the person
void introduce() {
cout << "Hi, my name is " << name << " and I am " << age << " years old." << endl;
}
};
int main()
{
// Create an object of the Person class
Person person1; // accessing data members
person1.name = "Alice";
person1.age = 30; /
/ Call the introduce member method
person1.introduce();
return 0;
}

Output:
Hi, my name is Alice and I am 30 years old.
Access Modifiers
• In C++ classes, we can control the access to the members of the class using
Access Specifiers. Also known as access modifier, they are the keywords
that are specified in the class and all the members of the class under that
access specifier will have particular access level.
In C++, there are 3 access specifiers that are as follows:
• Public: Members declared as public can be accessed from outside the class.
• Private: Members declared as private can only be accessed within the class
itself.
• Protected: Members declared as protected can be accessed within the class
and by derived classes.
• If we do not specify the access specifier, the private specifier is applied to
every member by default.
Interface and implementation of a class
• Interfaces are nothing but a way to describe the behavior of a class without
committing to the implementation of the class. In order to create an interface, we
need to create an abstract class which is having only pure virtual methods. In C++,
Interfaces are also called pure abstract classes.
• Pure Virtual Functions
A Pure Virtual Function is a function where we only declare the function but not
the function definition. The implementation for pure virtual methods is done at the
derived class by method/function overriding. A function is said to be a pure virtual
function if it is defined in the class as follows:
virtual datatype functionName(parameter1, parameter2,…) = 0
• Abstract Class
An abstract class is a class that is specially designed to be used as a base class. Abstract class must
have at least one pure virtual function. It may have variables and normal functions. The derived
classes of an abstract class must implement all the pure virtual functions of their base class or else
they too become abstract.

• Importance of Interfaces
Any class derived from the pure abstract class (Interface) must implement all of the methods of the
base class i.e. Interface.
Interface pointers can be passed to functions and classes thereby we can call the functions of the
derived class from there itself.

Rules While Using Interfaces


• Declare only pure virtual functions. (No definition)
• For pure virtual functions assign only 0.
• Cannot create an instance of the class.
• We can create a pointer to the instance of the derived class with a reference of a base abstract class.
Object Composition-Delegation
 An object is a basic unit of Object-Oriented Programming and represents real-life entities. Complex objects
are objects that are built from smaller or a collection of objects. This process of building complex
objects from simpler ones is called object composition. In object-oriented programming languages, object
composition is used for objects that have a “has-a” relationship with each other. Therefore, the complex
object is called the whole or a parent object whereas a simpler object is often referred to as a child object.
 Syntax:
class A
{
// body of a class
};
class B
{
A objA;
public:
B(arg-list) : objA(arg-list1);
};
In the classes given above, B uses objects of class A as its data members. Hence, B is a complex class that
uses a simple class A.
Types of Object Compositions:
There are two basic subtypes of object composition:
 1. Composition: The composition relationships are part-whole relationships where a part can only be a
part of one object at a time. This means that the part is created when the object is created and destroyed
when the object is destroyed. To qualify as a composition, the object and a part must have the following
relationship-
• The part (member) is part of the object (class).
• The part (member) can only belong to one object (class).
• The part (member) has its existence managed by the object (class).
• The part (member) does not know about the existence of the object (class).

 2. Aggregation: The aggregation is also a part-whole relationship but here in aggregation, the parts can
belong to more than one object at a time, and the whole object is not responsible for the existence of the
parts. To qualify as aggregation, a whole object and its part must have the following relationships:
• The part (member) is part of the object (class).
• The part (member) can belong to more than one object (class) at a time.
• The part (member) does not have its existence managed by the object (class).
• The part (member) does not know about the existence of the object (class).
Object Delegation
Object Delegation means using the object of another class as a class
member of another class. It is known as object delegation. Delegation can
be an alternative to inheritance, but in an inheritance, there is an i-s a
relationship, but in the delegation, there is no inheritance relationship
between the classes.

Benefits of object delegation:


• The reuse class can be changed without changing the reusing class.
• Multiple reusing classes can share reused class variables simultaneously.
• Variables and methods of reusing and the reused classes can be on separate
computers.
• It works in single inheritance languages.
Abstraction in C++
• Abstraction means displaying only essential information and ignoring the details.
Data abstraction refers to providing only essential information about the data to the
outside world, ignoring unnecessary details or implementation.
• Consider a real-life example of a man driving a car. The man only knows that
pressing the accelerator will increase the speed of the car or applying brakes will
stop the car but he does not know how on pressing the accelerator the speed is
actually increasing, he does not know about the inner mechanism of the car or the
implementation of the accelerator, brakes, etc in the car. This is what abstraction is.

Types of Abstraction:
• Data abstraction – This type only shows the required information about the data
and ignores unnecessary details.
• Control Abstraction – This type only shows the required information about the
implementation and ignores unnecessary details.
Encapsulation
Encapsulation in C++ is defined as the wrapping up of data and information
in a single unit. In Object Oriented Programming, Encapsulation is defined
as binding together the data and the functions that manipulate them.
Two Important property of Encapsulation
• Data Protection: Encapsulation protects the internal state of an object by
keeping its data members private. Access to and modification of these data
members is restricted to the class’s public methods, ensuring controlled and
secure data manipulation.
• Information Hiding: Encapsulation hides the internal implementation
details of a class from external code. Only the public interface of the class is
accessible, providing abstraction and simplifying the usage of the class
while allowing the internal implementation to be modified without
impacting external code.
Features of Encapsulation

Below are the features of encapsulation:


• We can not access any function from the class directly. We need an object
to access that function that is using the member variables of that class.
• The function which we are making inside the class must use only
member variables, only then it is called encapsulation.
• If we don’t make a function inside the class which is using the member
variable of the class then we don’t call it encapsulation.
• Encapsulation improves readability, maintainability, and security by
grouping data and methods together.
• It helps to control the modification of our data members.
Data Hiding
• Data hiding is an object-oriented programming (OOP) technique
specifically used to hide internal object details (i.e., data members).
Data hiding guarantees exclusive data access to class members only
and protects and maintains object integrity by preventing intended or
unintended changes and intrusions.
• The data members and the member functions in a class are made
private using data hiding so that they cannot be accessed falsely by
functions outside the class and are protected from accidental
modifications and changes.
Inheritance
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++.

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.
• Example Type of inheritance
• // Base class
class Vehicle {
public: • Single Inheritance
string brand = "Ford";
void honk() {
• Multiple Inheritance
cout << "Tuut, tuut! \n" ; • Multilevel Inheritance
}
};
• Hierarchical Inheritance
• Hybrid Inheritance
// Derived class
class Car: public Vehicle {
public:
string model = "Mustang";
}; Access Control and Inheritance
A derived class can access all the non-
int main() { private members of its base class. Thus
Car myCar;
myCar.honk();
base-class members that should not be
cout << myCar.brand + " " + myCar.model; accessible to the member functions of
return 0; derived classes should be declared private
} in the base class.
• When deriving a class from a base class, the base class may be inherited
through public, protected or private inheritance.
• We hardly use protected or private inheritance, but public inheritance is commonly
used. While using different type of inheritance, following rules are applied −
• Public Inheritance − When deriving a class from a public base class, public members
of the base class become public members of the derived class and protected members of
the base class become protected members of the derived class. A base
class's private members are never accessible directly from a derived class, but can be
accessed through calls to the public and protected members of the base class.
• Protected Inheritance − When deriving from a protected base
class, public and protected members of the base class become protected members of the
derived class.
• Private Inheritance − When deriving from a private base
class, public and protected members of the base class become private members of the
derived class.
When to use Inheritance in C++

• When you have a class hierarchy: If you have a group of related


classes, and there is a considerable amount of code that is shared by all
of them, then inheritance is an acceptable option.
• When you want to construct polymorphic classes: Inheritance is an
excellent choice if you want to create classes that can respond to the
same message in multiple ways.
• When you need to organize your code: If you wish to organize your
code more logically and understandably, inheritance might be a useful
concept.
Polymorphism
The word “polymorphism” means having many forms. In simple words,
we can define polymorphism as the ability of a message to be displayed
in more than one form. A real-life example of polymorphism is a person
who at the same time can have different characteristics. A man at the
same time is a father, a husband, and an employee. So the same person
exhibits different behavior in different situations. This is called
polymorphism. Polymorphism is considered one of the important
features of Object-Oriented Programming.
Types of Polymorphism
• Compile-time Polymorphism
• Runtime Polymorphism
1. Compile-Time Polymorphism
This type of polymorphism is achieved by function overloading or operator overloading.

A. Function Overloading
When there are multiple functions with the same name but different parameters, then the
functions are said to be overloaded, hence this is known as Function Overloading.
Functions can be overloaded by changing the number of arguments or/and changing the
type of arguments. In simple terms, it is a feature of object-oriented programming
providing many functions that have the same name but distinct parameters when
numerous tasks are listed under one function name.

B. Operator Overloading
C++ has the ability to provide the operators with a special meaning for a data type, this
ability is known as operator overloading. For example, we can make use of the addition
operator (+) for string class to concatenate two strings. We know that the task of this
operator is to add two operands. So a single operator ‘+’, when placed between integer
operands, adds them and when placed between string operands, concatenates them.
2. Runtime Polymorphism
This type of polymorphism is achieved by Function Overriding. Late binding and dynamic
polymorphism are other names for runtime polymorphism. The function call is resolved at
runtime in runtime polymorphism. In contrast, with compile time polymorphism, the compiler
determines which function call to bind to the object after deducing it at runtime.
A. Function Overriding
Function Overriding occurs when a derived class has a definition for one of the member
functions of the base class. That base function is said to be overridden.
B. Virtual Function
A virtual function is a member function that is declared in the base class using the keyword
virtual and is re-defined (Overridden) in the derived class.
Some Key Points About Virtual Functions:
Virtual functions are Dynamic in nature.
They are defined by inserting the keyword “virtual” inside a base class and are always declared
with a base class and overridden in a child class
A virtual function is called during Runtime
Static Data Members
• Static data members are class members that are declared using static keywords. A static
member has certain special characteristics which are as follows:
• Only one copy of that member is created for the entire class and is shared by all the
objects of that class, no matter how many objects are created.
• It is initialized before any object of this class is created, even before the main starts
outside the class itself.
• It is visible can be controlled with the class access specifiers.
• Its lifetime is the entire program.

Syntax
className {
static data_type data_member_name;
.....
}
Static Data Member
As told earlier, the static members are only declared in the class declaration. If we
try to access the static data member without an explicit definition, the compiler
will give an error.
To access the static data member of any class we have to define it first and static
data members are defined outside the class definition. The only exception to this
are static const data members of integral type which can be initialized in the class
declaration.
Syntax
datatype class_name::var_name = value...;
For example, in the above program, we have initialized the static data member
using the following statement:
int A::x = 10
Note: The static data members are initialized at compile time so the definition of
static members should be present before the compilation of the program
Accessing a Static Member
We can access the static data member without creating the instance of the class. Just remember that we need to
initialize it beforehand. There are 2 ways of accessing static data members:

1. Accessing static data member using Class Name and Scope Resolution Operator
The class name and the scope resolution operator can be used to access the static data member even when there
are no instances/objects of the class present in the scope.
Syntax
Class_Name :: var_name
Example
A::x

2. Accessing static data member through Objects


We can also access the static data member using the objects of the class using dot operator.
Syntax
object_name . var_name
Example
obj.x
Note: The access to the static data member can be controlled by the class access modifiers.
Friend Class
• 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.
We can declare a friend class in C++ by using the friend keyword.
Syntax:
• friend class class_name; // declared in the base class
Note: We can declare friend class or function anywhere in the base
class body whether its private, protected or public block. It works all the
same.
Friend Function
Like a friend class, a friend function can be granted special access to
private and protected members of a class in C++. They are not the
member functions of the class but can access and manipulate the private
and protected members of that class for they are declared as friends.
A friend function can be:
A global function
A member function of another class
Syntax:
friend return_type function_name (arguments); // for a global function
or
friend return_type class_name::function_name (arguments); // for a member
function of another class
Features of Friend Functions
 A friend function is a special function in C++ that in spite of not being a
member function of a class has the privilege to access the private and
protected data of a class.
 A friend function is a non-member function or ordinary function of a class,
which is declared as a friend using the keyword “friend” inside the class. By
declaring a function as a friend, all the access permissions are given to the
function.
 The keyword “friend” is placed only in the function declaration of the friend
function and not in the function definition or call.
 A friend function is called like an ordinary function. It cannot be called
using the object name and dot operator. However, it may accept the object as
an argument whose value it wants to access.
 A friend function can be declared in any section of the class i.e. public or
private or protected.
Empty classes
• It is a class that does not contain any data members (e.g. int a, float b,
char c, and string d, etc.) However, an empty class may contain member
functions.
• In C++, the Size of an empty structure/class is one byte as to call a
function at least empty structure/class should have some size (minimum 1
byte is required ) i.e. one byte to make them disti
Why actually an empty class in C++ takes one byte?
Simply a class without an object requires no space allocated to it. The
space is allocated when the class is instantiated, so 1 byte is allocated by
the compiler to an object of an empty class for its unique address
identification.
Program of empty class
#include <iostream>
using namespace std;
// Creating an Empty Class
class Empty_class {
};
// Driver Code
int main()
{
cout << "Size of Empty Class is = "
<< sizeof(Empty_class);
return 0;
}
Output
Size of Empty Class is = 1
Nested classes
A nested class is a class which is #include<iostream>
declared in another enclosing class. A using namespace std;
nested class is a member and as such
class A {
has the same access rights as any other
member. The members of an enclosing public: class B
class have no special access to {
members of a nested class; the usual private:
access rules shall be obeyed.
int num;
public:
void getdata(int n)
{
num = n;
}
void putdata()
Why use nested classes?
{
cout<<"The number is "<<num;
}
It enable you to logically group classes
that are only used in one place,
}; increase the use of encapsulation, and
}; create more readable and maintainable
code.
int main()
{
cout<<"Nested classes in C++"<< endl;
A :: B obj;
obj.getdata(9);
obj.putdata();
return 0;
}
Local classes
• A class declared inside a function becomes local to that function and is
called Local Class in C++.
• A local class name can only be used locally i.e., inside the function
and not outside it.
• The methods of a local class must be defined inside it only.
• A local class can have static functions but, not static data members.
For example, in the following program, Test is a
local class in fun().
• Following are some interesting
facts about Local Classes in C+
#include <iostream>
+:
1) A local class type name can only be used in
using namespace std; // Creating the class
the enclosing function.
void fun()
2) All the methods of Local classes must be
{ defined inside the class only.
// local to fun
class Test {
// members of Test class
};
}
// Driver Code
int main()
{
return 0;
}
Abstract classes
An abstract class is a class that is designed to be specifically used as a
base class. An abstract class contains at least one pure virtual function.
You declare a pure virtual function by using a pure specifier (= 0) in the
declaration of a virtual member function in the class declaration.

The following is an example of an abstract class:


class AB {
public:
virtual void f() = 0;
};
Container classes
We can create an object of one class into another and that object will be a member of the class. This type
of relationship between classes is known as containership or has_a relationship as one class contain the
object of another class. And the class which contains the object and members of another class in this kind
of relationship is called a container class.
• The object that is part of another object is called contained object, whereas object that contains
another object as its part or attribute is called container object.

 Difference between containership and inheritance


• Containership
When features of existing class are wanted inside your new class, but, not its interface
for eg->
1)computer system has a hard disk
2)car has an Engine, chassis, steering wheels.
• Inheritance
When you want to force the new type to be the same type as the base class.
for eg->
1)computer system is an electronic device
2)Car is a vehicle
Syntax for Containership:

// Class that is to be contained


class first
{
..
};
// Container class
class second
{
// creating object of first
first f;
..
};
Bit Fields
Bit Field is a feature in C++ for specifying the size of struct and class members so
that they occupy a specific number of bits, not the default memory size.
(When we declare the members in a struct and classes, a pre-determined size is
allocated for the member variables. In C++, we can compress that allocated size using
bit fields.)

• Bit-Field Syntax in Classes


class ClassName
{
public:
dataType fieldName : width;
// more bit-field members...
};
where width is the number of bits.

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