Objects
In OOPS, Objects are variables of classes.
Object
consists of the data and the functions that operate on the data. Object data is
usually private: no functions other than those within the same object can
access it. On the other hand, the functions within the object are usually
public. They can be accessed by the other functions in the program including
the functions in the other objects. So the function in one object can access
the data in the other object through the function of that object.
Class
It is a
specification for any number of objects based on that class. Class itself is a
template (data type) for objects. When we actually define the object, of a
class we are requesting to allocate memory for that object to hold its data.
Data Member
The data
items within a class are called data members. There can be any number of data
members in a class. Normally, data members follow the keyword private, so they
can be accessed from within the class but not from outside. That is why we say
that oops have feature of data hiding. So, it is safe from accidental
alteration.
class demo
{
private:
int rollno;
//member data
float score;
//member data
public:
setdata( int
rl, float sc) //member function
{rollno=rl;
score=sc;}
setdata( )
{
cout<<"Enter
the roll no: \n";
cin>>rollno;
cout<<"Score:
\n";
cin>>score;
}
showdata( ) //member
function
{
cout<<"\Roll
No: " <<rollno<<"has scored
"<<score<<endl;
}
};
above class
contains two data items: rollno and score. Here rollno is of type int and score
is of type float.
Member Function
Member
functions are functions that are included within a class. In above example
there are two member functions in class demo: setdata( ) and showdata( ). Each
function can have one or more statements. The functions setdata( ) and
showdata( ) follow the keyword public which means that they can be accessed
from outside the class. It is also possible to declare a function within a
class and define it elsewhere.
The class
demo can be used as follows
int main()
{
demo d1,d3;
d1.setdata(12,10.4);
d3.setdata(
);
d1.showdata(
);
d3.showdata(
);
return 0;
}
Call
In oops.
Objects are variables of classes. It is a specification for any number of
objects based on that class. Class itself is a template (data type) for
objects. When we actually define the object, of a class we are requesting to
allocate memory for that object to hold its data.
Some Examples
Write a
simple program that convert the temperature in degree Celsius to degree
Fahrenheit and vice versa using the basic concept of class and object. Make
separate class for Centigrade and Fahrenheit which will have the private member
to hold the temperature value and make conversion functions in each class for
conversion from one to other. For example you will have function toFahrenheit()
in class Celsius that converts to Fahrenheit scale and returns the value.
Assume that
you want to check whether the number is prime or not. Write a program that asks
for a number repeatedly. When it finishes the calculation the program asks if
the user wants to do another calculation. The response can be 'y' or 'n'. Don't
forget to use the object class concept.
Create a
class called carpark that has int data member for car id, int data
member for charge/hour and float data member for time. Set the data
and show the charges and parked hours of corresponding car id. Make two member
functions for setting and showing the data. Member function should be called
from other functions.
Write a
program with classes to represent circle, rectangle and triangle. Each classes
should have data members to represent the actual objects and member functions to
read and display objects, find perimeter and area of the objects and other
useful functions. Use the classes to create objects in your program.
Feel
Free To Comment Some Question On this Topics
No comments:
Post a Comment