Docsity

Classes & Objects in Object-Oriented-Programming, Lecture notes of Object Oriented Programming

Partial preview of the text

Download Classes & Objects in Object-Oriented-Programming and more Lecture notes Object Oriented Programming in PDF only on Docsity!

Fundamentals of object

oriented programming

Class : BS(AI)-II (2022-26) Course Instructor : Adam Curi Department of CS & IT University of Azad Jammu & Kashmir

Summary of previous lecture

 (^) Programmer thinks about and defines the attributes and behavior of objects.  (^) Often the objects are modeled after real-world entities.  (^) Very different approach than function-based programming.  (^) Object-oriented programming (OOP)  (^) Encapsulates data (attributes) and functions (behavior) into packages called classes.  (^) So, Classes are user-defined (programmer-defined) types.  (^) Data (data members)  (^) Functions (member functions or methods)

What is Class?

 A class is a user-defined layout of an object that describes what a

specific kind of object will look like.

 The mechanism that allows you to combine data and the function

in a single unit is called a class.

 Once a class is defined, you can declare variables of that type. A

class variable is called object or instance.

 In other words, a class would be the data type, and an object

would be the variable. Classes are generally declared using the

keyword class.

 OOP with C++ program can have any number of classes. When

related data and functions are kept under a class, it helps to

visualize the complex problem efficiently and effectively.

What is Class?  (^) A class is a definition of an object. It's a type just like int.  (^) A class resembles a struct with few differences : all struct members are public by default while All classes members are private.  (^) (Home Work. Oral test will be taken)   (^) Remember : A class is a type, and an object of this class is just a variable.

Classes  (^) A class is used as a template or pattern to create new objects (instances of the class)  (^) Every object is an instance of a class

 each object has its own name (identity)

 each object has its own variables (state)

 all instances of the class share the methods defined by the

class (behaviours)

Classes in C++  (^) A class definition begins with the keyword class.  (^) The body of the class is contained within a set of braces, { } ; (notice the semi-colon). class class_name { …. …. …. }; Class body (data member

  • methods) Any valid identifier

Access Specifiers  (^) public - members are accessible from outside the class  (^) private - members cannot be accessed (or viewed) from outside the class  (^) protected - members cannot be accessed from outside the class, however, they can be accessed in inherited classes. You will learn more about Inheritance later.  (^) Within the body, the keywords private: and public: specify the access level of the members of the class.  (^) the default is private.  (^) Usually, the data members of a class are declared in the private: section of the class and the member functions are in public: section.

Access Specifiers private , protected or public (by default access to members of a class is private ). OR class class_name { private: members1; protected: members2; public: members3; };

Defining Class private members of a class are accessible only from within other members of the same class. You cannot access it outside of the class. protected members are accessible from members of their same class and also from members of their derived classes. protected members are accessible from other members of the same class (or from their "friends" ), but also from members of their derived classes.

Defining Class Finally, public members are accessible from anywhere where the object is visible. By default, all members of a class declared with the class keyword have private access for all its members. Therefore, any member that is declared before one other class specifies automatically has private access.

Defining Class Example class Circle main() { { private: double radius; Circle c; public: void setRadius(double r) c.setRadius(3.2); { radius = r; } double rad=c.getArea(); double getArea() } { return 3.14 * radius * radius; } };

Defining Member Function of Class Member functions can be defined in two ways,

**1. Inside the class

  1. Outside the class**

Defining Member Function of Class Outside the class A large program may contain many member functions. For the clarity of the code, member functions can be defined outside the class. To do so, member function should be declared inside the class(function prototype should be inside the class). Then, the function definition can be defined using scope resolution operator ::.

Defining Member Function of Class Outside the class When we define a function outside the class we cannot reference them (directly) outside of the class. In order to reference these, we use the scope resolution operator, :: (double colon). In this example, we are defining function setRadius outside the class: void Circle :: setRadius(double r) { radius = r; }