Sie sind auf Seite 1von 24

Basic Concepts of OOPs

Basic Concepts of Object-Oriented


Programming
• Objects
• Classes
• Data abstraction and encapsulation
• Inheritance
• Polymorphism
• Dynamic binding
• Message passing
Objects

• Objects are the basic run-time entities in an object-oriented


system.
• They may represent a person, a place, a bank account, a table
of data or any item that the program must handle.
• Program objects should be chosen such that they match closely
with the real-world objects.
• Objects take up space in the memory and have an associated
address like structure in C.
• When a program is executed the objects interact by sending
messages to one another.
Way to present an object
Object A Private Object B

Data Data

Communication
Functions Functions

Public

Object C

Functions

Data

Organization of data and functions in OOP


Object

Operation

Operation Attributes Operation

Operation
Example: Student Object

Enroll()

st_name
st_id
Performa Displayinfo()
branch
nce()
semester

Result()
Class
• Class is a collection of similar objects.

Class
classes

• The entire set of data and code of an object can be made a


user-defined data type with the help of a class.
• Objects are variable of type class.
• Once a class has been defined we can create any number of
objects belonging to that class.
• A class is thus a collection of objects of similar type.
• Classes are user-defined data types and behave like the built-in
types of a programming language.
classes

• Syntax used to create an object is no different than the syntax


used to create an integer object in C.
• Fruit is a class defined as a Class.
• Fruit f1;

• Here fruit is Class.


• And f1 is an object of fruit Class.
A Simple Class

class item
{
int number; // variables declaration
float cost; // private by default
Public:
void getdata(int a, float b); // functions declaration
void putdata(void); // using prototype
}; // ends with semicolon
• Attributes also called data members
– Because they hold information.

• Functions that operate on these data are called methods or


member functions.
“Smith”;
// an example with class void person :: getdata(void)
#include<iostream.h> {
using namespace std; cout<<“enter name: ”;
class person cin>>name;
cout<<“enter age: ”;
{ cin>>age;
char name[30]; }
int age; void person :: display(void)
public: {
void getdata(void); cout<<“\n Name: “<<name;
cout<<“\n Age: “<<age;
void display(void); }
};
int main()
{
person p;
p.getdata();
p.display();
return 0;
}
Output:
Enter name: raja
Enter age: 30
Name : raja
Age : 30
Class Definition - Access Control

• Information hiding
– To prevent the internal representation from direct access
from outside the class
• Access Specifiers
– public
• may be accessible from anywhere within a program
– private
• may be accessed only by the member functions, and friends of this
class
– protected
• acts as public for derived classes
• behaves as private for the rest of the program

19
#include<iostream.h> int main( )
using namespace std; {
class smallobj smallobj s1,s2;
{ s1.setdata(1066);
int data; s2.setdata(1776);
public:
void setdata(int d); s1.showdata();
{ data = d; } s2.showdata();
void showdata( ); return(0);
{ cout<<data; } }
};
#include<iostream.h> int main( )
using namespace std; {
class part part part1;
{ part1.setpart(624,200);
int modelno;
part2.setpart(177,300);
float cost;
public:
void setpart(int mn,float c); part1.showpart();
{ modelno = mn; part2.showpart();
cost = c; } return(0);
void showpart( ); }
{ cout<<modelno;
cout<<cost; }
};
#include<iostream.h>
int main( )
using namespace std;
{
class distance distance dist1,dist2;
{ dist1.setdist(11,6.2);
int feet;
float inches; dist2.getdist( );
public:
void setdist(int ft,float in); cout<<dist1.showdist();
{ feet = ft;
inches = in; } cout<<dist2.showdist();
return(0);
void getdist( ); }
{ cin>>feet;
cint>>inches; }

void showdist()
{ cout<<feet<<inces; }
};
Another Example
#include <iostream.h> // member function definitions

class circle void circle::store(double r)


{
{ radius = r;
private: }
double radius;
double circle::area(void)
{
public: return 3.14*radius*radius;
void store(double); }
double area(void);
void display(void); void circle::display(void)
{
cout << “r = “ << radius << endl;
}; }

int main(void) {
circle c; // an object of circle class
c.store(5.0);
cout << "The area of circle c is " << c.area() << endl;
c.display();
24
}

Das könnte Ihnen auch gefallen