
















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!

















Class : BS(AI)-II (2022-26) Course Instructor : Adam Curi Department of CS & IT University of Azad Jammu & Kashmir
(^) 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?
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
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
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
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; }