Ec-204 Data Structures & Object Oriented Programming: Dr. Waqar Shahid LE Arshia Arif
The document discusses the concepts of classes, objects, constructors, destructors, and data encapsulation in C++. It defines a class as a blueprint that combines both data and functions that operate on the data. The document also explains how classes use access specifiers like public, private, and protected to control access to data members and member functions for encapsulation and data hiding.
Download as PPTX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
47 views
Ec-204 Data Structures & Object Oriented Programming: Dr. Waqar Shahid LE Arshia Arif
The document discusses the concepts of classes, objects, constructors, destructors, and data encapsulation in C++. It defines a class as a blueprint that combines both data and functions that operate on the data. The document also explains how classes use access specifiers like public, private, and protected to control access to data members and member functions for encapsulation and data hiding.
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16
EC-204 DATA STRUCTURES
& OBJECT ORIENTED PROGRAMMING
Dr. Waqar Shahid
LE Arshia Arif Class • A class is an expanded concept of C structure: instead of holding only data, it can hold both data and functions. (now C structs can have functions too but everything is “public” in structs) Class The fundamental idea is to combine into a single unit both data and functions that operate on the data. • Such a unit is called an “Object”. • The definition of this unit is called a “Class”. • An object’s functions are called “member functions” (behaviors) in C++. • And its data are called “data members” (attributes).
An object is an instantiation of a class. In terms of variables, a class would be the type, and an object would be the variable Class Syntax: class class_name { access_specifier_1(public/private/ protected): Data member; access_specifier_2(public/private/protected): Member functions; ... }; Class Access Specifiers: An access specifier is one of the following three keywords: • Private • Public • Protected These specifiers modify the access rights that the members following them acquire
Class Main Parts of a Class: • Data Members • Constructor • Member Functions • Destructor Class(Example) Constructor • Initializes an object automatically as soon as the object is created • Must have the same name as that of the class • Constructors have No return type • Constructors are called at the point an object is created • If a class has a constructor, each object of that type is initialized with the constructor prior to use in a program • Eliminates the need to add the setdata() function Constructor(Syntax) Two methods of writing the constructor: • Counter(){count=0;} OR • Counter():count(0){};//initializerlist Destructor • Destructor functions are the inverse of constructor functions. • As constructors, destructor functions are also called automatically when an object is destroyed • Same name as the class, preceded by tilde(~) • No return values and No arguments • The most common use of destructors is to de- allocate memory that was allocated for the object. • Should be used before object goes out of scope Destructor(Example) Object • To use a class in a C++ program, its objects must be instantiated • Similar to declaring a variable • Example: Data Encapsulation All C++ programs are composed of the following two fundamental elements − • Program statements (code) − This is the part of a program that performs actions and they are called functions. • Program data − The data is the information of the program which gets affected by the program functions. Data Encapsulation • Encapsulation is an Object Oriented Programming concept that binds together the data and functions that manipulate the data, and that keeps both safe from outside interference and misuse. Data encapsulation led to the important OOP concept of data hiding. • C++ supports the properties of encapsulation and data hiding through the creation of user-defined types, called classes. We already have studied that a class can contain private, protected and public members. • By default, all items defined in a class are private. Data Encapsulation(Example) Data Encapsulation(Example contd.)
• The private member total is something that is
hidden from the outside world, but is needed for the class to operate properly.