P1 – Features of OOP
Object
An object is known as a ‘thing’ is used to relate actiitese The actiites which objects are used
perform decides the object behaiioure One example would be the foot is able to kick somethinge
In OOP (object oriented programming) an object is an instance of a classe
For example:
Box box1;
Box Box2;
Classes
A class represents a type of objecte It is a blueprint which will giie details of the objecte A class is
a blueprint in which indiiidual objects are madee Class contains: names, atributes, and
operatonse
For example
class box
{
};
Inheritance
Inheritance is being able to make a new class from an already existng class by expanding on ite
Inheritance allows programmers to defne a class in terms of another class which makes it
simple to make and maintain applicatonse hich proiides the chance to reuse code
functonality and implement quuickere
Example:
class circle
circle circle1
Encapsulation
Encapsulaton is the additon in a program object requuired for a functon or objecte In OOP
encapsulaton is usually done by making classes, the classes show public methods and
propertese A class is a capsule which encapsulates methods, atributes and propertese To
proiide functons to other classese Encapsulaton also lets a class to change the internal
implementaton without damaging the oierall functon of the systeme
Example:
class rectangle {
,public:
double get area(ioid)
{
return length * height;
}
priiate:
double length
double height;
};
Polymorphism
Polymorphism is a term which means being able to requuest same operatons by a range of types
or thingse In OOP polymorphisms is done by using diferent techniquues method, oierloading,
operator oierloading, and method oierridinge
class shape {
Public:
Double height
Double weight
Double area
};
Class squuare: public shape
Class rectangle: public shape
Int main()
{
Squuare squuare2
Squuare2eheight = 5e5
Squuare2ewidth = 4e5
area = squuare2eheight * squaure2ewidth
rectangle2eheight = 3e5
rectangle2ewidth = 6e5
area = rectangle2eheight * rectangle2ewidth
]:
, Data abstraction
Abstracton priorites the idea, quualites and propertes than partcularse The importance of
abstracton is depriied from abilites to hide irreleiant details from use of names to reference
objectse Abstracton is used to construct programse It priorites what an object is rather than
how the object is represented or how the object workse
#include <iostream>
using namespace std;
Int main ( )
{
cout << “Hello MY NAME IS RAJ”<<endl;
return 0;
}
P2 - Demonstrate the use of object oriented tools and
techniques
Program 1 code:
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
int firstnumber, secondnumber, output;
cout << "Input first number:"";
cin >> firstnumber;
cout << "Input second number:"";
cin >> secondnumber;
cout << "What is the answer to this equation:" " << firstnumber << " + " <<
secondnumber << " ";
cin >> output;
if (output == firstnumber + secondnumber)
{
cout << "Answer is correct, Good job.";
system("pause");
}
else
{
cout << "Answer is incorrect, the correct answer was:" " << firstnumber +
secondnumber << "\n";
system("pause");
}
return 0;