Institute of Business & Technology (BIZTEK) OBJECT-ORIENTED PROGRAMMING CLASSES & OBJECTS
CLASS A class is just a collection of variables--often of different types--combined with a set of related functions. The variables in the class are referred to as the member variables or data members. The functions in the class typically manipulate the member variables. They are referred to as member functions or methods of the class.
Member variables , also known as data members , are the variables in your class. Member variables are part of your class. Member functions , also known as methods , are the functions in your class. Member functions are as much a part of your class as the member variables. They determine what the objects of your class can do.
To declare a class, use the class keyword followed by an opening brace, and then list the data members and methods of that class. End the declaration with a closing brace and a semicolon. Here's the declaration of a class called Cat: class Cat { unsigned int itsAge; unsigned int itsWeight; abc(); };
OBJECT An object is an individual instance of a class. You define an object of your new type just as you define an integer variable: unsigned int GrossWeight; // define an unsigned integer Cat Frisky; // define a Cat This code defines a variable called Gross Weight whose type is an unsigned integer. It also defines Frisky, which is an object whose class (or type) is Cat. Object are sometimes called instance variables.
Accessing Class Members Once you define an actual Cat object--for example, Frisky--you use the dot operator (.) to access the members of that object. Therefore, to assign 50 to Frisky's Weight member variable, you would write Frisky.Weight = 50;
The body of the class contains two unfamiliar keywords: private and public The key feature of the object-oriented programming is data hiding. It means that data is concealed within a class, so that It cannot be accessed mistakenly by functions outside the class. The primary mechanism for hiding data is to put it in a class and make it private. Private data or functions can only be accessed from within the class. Public data or functions are accessible from outside the class
Output: Frisky is a cat who is 5 years old. #include <iostream.h> class Cat { public: int itsAge; 10: int itsWeight; }; void main() Cat Frisky; Frisky.itsAge = 5; cout << "Frisky is a cat who is " ; cout << Frisky.itsAge << " years old.\n"; } Output: Frisky is a cat who is 5 years old.
INHERITANCE Inheritance is the process of creating new classes, called derived classes from existing or base classes. The derived class inherits all the capabilities of the base class. class Cat { unsigned int itsAge; unsigned int itsWeight; abc(); }; class scat : public cat
class employee class manager class scientist class laborer NAME ID TITLE CLUB DUES PUBLICATIONS
Class employee { Private: char name[20]; int number; Public: void getdata() cout<<“\n ENTER NAME”; cin>>name; cout<<“ENTER NUMBER”; cin>>number; } void putdata() cout << “\n Name: “ <<name cout << “\n Number: “ << number; };
Class manager : public employee { Private: char title[20]; int dues; Public: void getdata() employee::getdata(); cout<<“\n ENTER TITLE”; cin>>title; cout<<“ENTER CLUB DUES”; cin>>dues; } void putdata() employee::pudata(); cout << “\n TITLE: “ << title cout << “\n CLUB DUES: “ << dues; };
Class scientist : public employee { Private: int pubs; Public: void getdata() employee::getdata(); cout<<“\n ENTER NUMBER OF PUBLICATIONS”; cin>>pubs; } void putdata() employee::pudata(); cout << “\n NUMBER OF PUBLICATIONS : “ << pubs };
Class laborer : public employee { };
main( ) { manager m1,m2; scientist s1; laborer l1; Count << endl; cout << “ENTER DATA FOR MANAGER 1 “; m1.getdata(); cout << “ENTER DATA FOR MANAGER 2 “; m2.getdata(); }
cout << “ENTER DATA FOR SCIENTIST “; s1.getdata(); cout << “ENTER DATA FOR LABORER “; l1.getdata(); cout << “DATA OF MANAGER 1 “; m1.putdata(); cout << “DATA OF MANAGER 2 “; m2.putdata(); }