classes and objects by dk mamonai
TRANSCRIPT
-
8/6/2019 Classes and Objects by Dk Mamonai
1/10
OBJECT: TOBECOMEFAMILIARWITH CLASSESAND 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 hasnumber 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.
-
8/6/2019 Classes and Objects by Dk Mamonai
2/10
Understanding the class and object in C++
In C++ you can create your class by using the keyword class so that the compiler canunderstand that the thing you are writing is a class. First write the keyword class andthen place the data items and the member functions with in the braces terminated bythe 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 timeyou perform this approach you can do inverse also.
Program (class1.cpp)
#include
class vehicle
{
private:
int modelno;int maxspeed;
public:
void getdata(int mn, int ms)
{
modelno=mn;
maxspeed=ms;
}
void showdata(){
cout
-
8/6/2019 Classes and Objects by Dk Mamonai
3/10
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 keywordpublic
is used when we want that the data or function should be accessed from any where
outside the class. The keywordprotectedis 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.
-
8/6/2019 Classes and Objects by Dk Mamonai
4/10
EXERCISE
1. Create a class calledRectangle that contains variables and functions to compute
the area and perimeter of rectangle. The class also contains functions to get data fromthe 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 sayaddition,
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 calledQuadraticEquation. The class contains the variables and
functions to solve the quadratic equation (ax2+bx+c=0).The class has getvalues()
anddisplayRoots()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 anarray. The program gets the data from the user in an array and displays the maximumand minimum value along with index of the array.
5. Create a class Matrixthat gets two matrices of same size from the user and
display the addition and multiplication of those matrices. The class contains allnecessary functions to calculate the operations.
-
8/6/2019 Classes and Objects by Dk Mamonai
5/10
EXERCISESOLUTIONS
===================================================
Program # 01
#include
class rectangle
{
private:
int length;
int height;
int Area;
int perimeter;
public:
void getdata(int ln, int hg)
{
ln=length;
hg=height;
Area=length*height;
perimeter=2*(length+height);
}
void showdata()
{
cout
-
8/6/2019 Classes and Objects by Dk Mamonai
6/10
-
8/6/2019 Classes and Objects by Dk Mamonai
7/10
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
const int SIZE=10;
class MaxMin
{
private:
int array[SIZE];
int temp;
public:
void getdata()
{
for(int i=0; i
-
8/6/2019 Classes and Objects by Dk Mamonai
8/10
if(array[i]>array[j])
{
temp=array[i];
array[i]=array[j];
array[j]=temp;
}
}
}
cout
-
8/6/2019 Classes and Objects by Dk Mamonai
9/10
{
for(j=0; j
-
8/6/2019 Classes and Objects by Dk Mamonai
10/10
cout