Programming Paradigms Lecturer Hamza Azeem Lecture 9 Objects
Programming Paradigms Lecturer Hamza Azeem
Lecture 9 Objects and Classes
Revision of Objects and Classes • What is an Object ? • An object is a single identity which compromises of following concepts; • characteristics • responsibilities (or behaviors) • Or simply Object = Data + Function
Classes • A class is like a cookie-cutter; it defines the shape of object • Objects are like cookies; they are instances of the class
Classes versus Objects • A class is a prototype specification from which one can generate a number of similar objects • A class can be considered as an object factory • An object is said to be a instance of a class
Example of a Class in C++ class student //declares a class { private: int id, age; //class data public: int getage() //method to get age of object { return (age); } }
Class and its Objects person data: name, address, age methods: getage() person data: Rashid, Tariq Road, 25 person data: Khalid, Clifton, 24 person data: Ali, N. Nazimabad, 28 Class
Class Example • This class example shows how we can encapsulate (gather) a circle information into one package (class) class Circle { private: double radius; public: void set. Radius(double r); double get. Diameter(); double get. Area(); double get. Circumference(); }; No need for others classes to access and retrieve its value directly. The class methods are responsible for that only. They are accessible from outside the class, and they can access the member (radius)
Creating an object of a Class • Declaring a variable of a class type creates an object. You can have many variables of the same type (class). – Creating Instance • Once an object of a certain class is created, a new memory location is created for it to store its data members and code • You can create many objects from a class type. – circle smallcircle; – circle bigcircle;
The two steps of Object Oriented Programming • Making Classes: Creating, extending or reusing abstract data types. • Making Objects interact: Creating objects from abstract data types and defining their relationships. 10
Creating Class • Here we will create a class “KIETStudent” which has following characteristics and behaviors • Name, Age, In. Take, CGPA • get. Age, get. Name, get. In. Take, get. CGPA
Creating Class class KIETStudent { public: int age; char *name; double cgpa; char* intake; };
Creating Instance class KIETStudent { public: char *name; int age; double cgpa; char *intake; }; int main() { // Create instane of class KIETStudent student 1; // Fill out the data student 1. age = 15; student 1. name = "Ali"; student 1. cgpa = 3. 12; student 1. intake = "FALL 09"; // Display the student data printf("Student's Data"); printf("n. Name: %s", student 1. name); printf("n. Age: %d", student 1. age); printf("n. Intake: %s", student 1. intake); printf("n. CPGA: %. 2 lf", student 1. cgpa); getch(); }
Public and Private Fields • We learned in previous lecture that basic characteristics should not be made public but rather private • So the characteristics should be transferred in private domain
Creating Class class KIETStudent { private: int age; char *name; double cgpa; char* intake; };
Class Methods (Functions) • Since the characteristics of object are made private we now need methods to modify and access them • These methods will be made public
class KIETStudent { private: int age; char *name; double cgpa; char *intake; Creating Methods int main() { // Create instane of class KIETStudent student 1; // Fill out the data student 1. set_age(20); public: void set_age(int newage); int get_age(); }; void KIETStudent: : set_age(int new_age) { age = new_age; printf("n. Age successfully set to %d. ", age); } int KIETStudent: : get_age() { return(age); } // Display the data printf("n. Student's Data"); printf("n. Age: %d", student 1. get_age() ); getch(); }
- Slides: 17