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 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. Properties of Objects An object has: state behaviours identity 6 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) 7 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; }; ACCESS SPECIFIERS The private members are not accessible outside the class; they can be accessed only through methods of the class. The public members form an interface to the class and are accessible outside the class. Instances of these data types are known as objects and can contain member variables, constants, member functions, and overloaded operators defined by the programmer. Classes in C++ class class_name { private: … … … public: … … … }; Public members or methods private members or methods 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 2. Outside the class 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; } Object Declaration Once a class is defined, you can declare objects of that type. The syntax for declaring a object is the same as that for declaring any other variable. The following statements declare two objects of type circle: Circle c1, c2; Circle is class and c1 and c2 are the objects. Once the class is defined you can define any number of objects. Object Declaration Accessing Class Members Once an object of a class is declared, it can access the public members of the class using ( . ) dot membership operator. c1.setRadius(2.5);