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

Objects and Classes

C++ Classes and Objects topic lecture

Uploaded by

qbilal.1303
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Objects and Classes

C++ Classes and Objects topic lecture

Uploaded by

qbilal.1303
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Classes and Objects

A class is a user-defined data type that we can use in our program,


and it works as an object constructor, or a "blueprint" for creating
objects.
When you define a class, you define a blueprint for a data type. This
doesn't actually define any data, but it does define what the class
name means, that is, what an object of the class will consist of and
what operations can be performed on such an object.
Terminologies
User Defined Data Type:
The data type that is a collection of variables of different data types. If u
remember, then array is a collection of variables of same data types. If the
requirement is to have a collection of variables of different data types, with
some functions that operate on those values. Then we have a solution: Classes
Data Members:
These are the variables of class, the values used. These can be public and
private.
Member Functions:
A member function of a class is a function that has its definition or its prototype
within the class definition like any other variable.
Class Access Modifiers
A class member can be defined as:
•Public
•private
•Protected
By default members would be assumed as private.
The public Members
A public member is accessible from anywhere outside the class but within a
program.

The private Members


A private member variable or function cannot be accessed, or even viewed from
outside the class. Only the class and friend functions can access private
members.
By default all the members of a class would be private.
Practically, we define data in private section and related functions in public
section so that they can be called from outside of the class.
Create a Class
To create a class, use the class keyword: •The class keyword is used to create a class called MyClass.
•The public keyword is an access specifier, which specifies that
members (attributes and methods) of the class are accessible
from outside the class.
•Inside the class, there is an integer variable myNum and a string
variable myString. When variables are declared within a class,
they are called attributes.
•At last, end the class definition with a semicolon ;
Create an Object
•In C++, an object is created from a class. We have
already created the class named MyClass, so now
we can use this to create objects.
•To create an object of MyClass, specify the class
name, followed by the object name.
•To access the class attributes (myNum and
myString), use the dot syntax (.) on the object:
Multiple You can create multiple
Objects objects of one class

You might also like