You are on page 1of 10

OBJECT: TO

BECOME FAMILIAR WITH

CLASSES

AND

OBJECTS.

Objects and Classes The objects and classes lie at the heart of the object oriented programming. As in procedural languages the main emphasis is on the functions but here in the object oriented programming the main emphasis is on the object and classes. It is an easy and prominent method through which we can compare and declare the physical objects in the programming. Object and the classes are the main features which are responsible for the popularity of an object oriented programming. Almost all the programmers give precedence to the object oriented programming than the procedural or structure programming because it provides the most convenient way to organize the program. The class is nothing more than the collection of the data items and the member functions. Once you have created a class you can create number of its objects. Each object will be now related directly or indirectly to that class. When you have created a class it will not allocate any memory space until you have declared any object of that class and each object will occupy same space in the memory unless it is the object of any other class. Object has the same relationship to the class as the variable has to its data type. An object is said to be the instance of the class. As physically on this earth there are the number of classes of things and each class has number of objects and each object has number of attributes. Like Jewelry is the class whose objects are the Earrings, Bracelets, Bangles, Rings, Necklaces, lockets etc. each object has the same relationship with the class and each has same attributes as they all are made from gold and they are used for wearing etc. Unlike C, C++ is the object oriented programming language. The C language almost is compatible with the C++ programming language but the C++ is not compatible with the C language.

Understanding the class and object in C++ In C++ you can create your class by using the keyword class so that the compiler can understand that the thing you are writing is a class. First write the keyword class and then place the data items and the member functions with in the braces terminated by the semicolon at the last. In general the data items are declared as the private and the member functions are declared as the private although it is not defined that every time you perform this approach you can do inverse also.

Program (class1.cpp)
#include <constream.h> class vehicle { private: int modelno; int maxspeed; public: void getdata(int mn, int ms) { modelno=mn; maxspeed=ms; } void showdata() { cout<<Model Number: <<modelno<<endl; cout<<Maximum speed: <<maxspeed<< km/h; } }; void main() { vehicle car, bus; car.getdata(2007,500); car.showdata(); bus.getdata(2004,100); bus.showdata(); getch(); }

In the above program a class vehicle is created in which the data items modelno and maxspeed are declared as private so that they can not be accidentally accessed any where outside the class and the member function getdata and showdata are declared as public so that they are accessible from out side the class. These member functions are also called the methods. In the main program the two objects car and bus are created so now they both will have the same function/methods that are declared in the class. To access the member function the dot operator is used between the object and the member function. The member function car.getdata() will set the data for the object car and the member function bus.getdata will set the data for the object bus; the member function car.showdata() will show the data of object car and the member function bus.showdata() will show data of the object bus.

Private, Public and Protected The keyword private is used when we want that the data should be protected from accidental accessed from outside the class or from the derived class. The keyword public is used when we want that the data or function should be accessed from any where outside the class. The keyword protected is used when we want that the data or function should be protected from the accidental access outside the class but it should be accessed from the derived class. In general the data is set as the private and the methods are declared as the private.

EXERCISE
1. Create a class called Rectangle that contains variables and functions to compute
the area and perimeter of rectangle. The class also contains functions to get data from the user to calculate the area and perimeter, and then a function prints out the calculated results.

2. Create a class Arithmetic that contains variables and functions say addition, subtraction, multiplication and division to compute the arithmetic
operations. In addition, the class also contains other functions for getting input from the user and to display the desired output.

3. Create a class called QuadraticEquation. The class contains the variables and functions to solve the quadratic equation (ax2+bx+c=0). The class has getvalues() and displayRoots() functions to get data from user and display roots of equations.
4. Write a program using classes that finds the maximum and minimum values of an array. The program gets the data from the user in an array and displays the maximum and minimum value along with index of the array.

5. Create a class Matrix that gets two matrices of same size from the user and
display the addition and multiplication of those matrices. The class contains all necessary functions to calculate the operations.

EXERCISE SOLUTIONS
===================================================

Program # 01
#include <constream.h> class rectangle { private: int int int int public: void getdata(int ln, int hg) { ln=length; hg=height; Area=length*height; perimeter=2*(length+height); } void showdata() { cout<<"The Area is: "<<Area<<endl; cout<<"The perimeters are: "<<perimeter; } }; void main() { rectangle R1; R1.getdata(4,5); R1.showdata(); getch(); }
===================================================

length; height; Area; perimeter;

===================================================

Program # 02
#include <constream.h> class arithmetic { private: int num1; int num2; char oper; int int int int public: void getdata(int n1, int n2, char opt) { num1=n1; num2=n2; oper=opt; } void showdata() { if(oper=='+')cout<<num1<<" + "<<num2<<" <<endl; else if(oper=='-')cout<<num1<<" - "<<num2<<" <<endl; else if(oper=='*')cout<<num1<<" * "<<num2<<" <<endl; else if(oper=='/')cout<<num1<<" / "<<num2<<" <<endl; } }; void main() { arithmetic Arith1, Arith2, Arith3, Arith4; add(int sub(int mul(int div(int n1, n1, n1, n1, int int int int n2){return(n1+n2);} n2){return(n1-n2);} n2){return(n1*n2);} n2){return(n1/n2);}

= "<<add(num1,num2) = "<<sub(num1,num2) = "<<mul(num1,num2) = "<<div(num1,num2)

Arith1.getdata(2,3,'+'); Arith2.getdata(5,3,'-'); Arith3.getdata(2,3,'*'); Arith4.getdata(8,4,'+'); Arith1.showdata(); Arith2.showdata(); Arith3.showdata(); Arith4.showdata(); getch(); }


===================================================

Program # 04
#include <constream.h> const int SIZE=10; class MaxMin { private: int array[SIZE]; int temp; public: void getdata() { for(int i=0; i<SIZE; i++) { cout<<"Enter Number "<<(i+1)<<" : "; cin>>array[i]; } } void showdata() { for(int i=0; i<SIZE-1; i++) { for(int j=0; j<SIZE; j++) {

if(array[i]>array[j]) { temp=array[i]; array[i]=array[j]; array[j]=temp; } } } cout<<"Maximum Number = "<<array[SIZE]<<endl; cout<<"Minimum Number = "<<array[0]<<endl; cout<<"Array index = "<<SIZE<<endl; } }; void main() { MaxMin MM1; MM1.getdata(); MM1.showdata(); getch(); }
===================================================

Program # 05
#include <constream.h> const int SIZE1=3; const int SIZE2=3; int i, j, k, temp=0; class Matrix { private: int int int int

MatA[SIZE1][SIZE2]; MatB[SIZE1][SIZE2]; MatC[SIZE1][SIZE2]; MatD[SIZE1][SIZE2];

void add() { for(i=0; i<SIZE1; i++)

{ for(j=0; j<SIZE2; j++) { MatC[i][j]=MatA[i][j]+MatB[i][j]; } } } void mult() { for(i=0; i<SIZE1; i++) { for(j=0; j<SIZE2; j++) { for(k=0; k<SIZE1; k++) { MatD[i][j]+=MatA[i][k]*MatB[k][i]; } } } } public: void getmat() { for(i=0; i<SIZE1; i++) { for(j=0; j<SIZE2; j++) { cout<<"Enter the Element A["<<(i+1)<<"]["<<(j+1)<<"] : "; cin>>MatA[i][j]; } } for(i=0; i<SIZE1; i++) { for(j=0; j<SIZE2; j++) { cout<<"Enter the Element B["<<(i+1)<<"]["<<(j+1)<<"] : "; cin>>MatB[i][j]; } } } void showmat() {

cout<<"Addition:"<<endl<<endl; add(); for(i=0; i<SIZE1; i++) { for(j=0; j<SIZE2; j++) { cout<<MatC[i][j]<<" "; } cout<<endl<<endl; } cout<<endl<<endl<<"Multiplication:"<<endl<<endl; mult(); for(i=0; i<SIZE1; i++) { for(j=0; j<SIZE2; j++) { cout<<MatD[i][j]<<" "; } cout<<endl<<endl; } } }; void main() { Matrix M1; clrscr(); M1.getmat(); M1.showmat(); getch(); }
===================================================

You might also like