We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 27
HANDWRITTEN
TOPIC
@ OBJECT AND CLASSES
@ POLYMORPHISM
[Bcso54 Object Oriented S
L ystem Design with C++
[Unit] pic
To}
Introduction: The meaning of Object Orientation, object identity, Encapsulation, information
1 _| hiding, polymorphism, generosity, importance of modelling, principles of modelling, object oriented
‘modelling, Intoduction to UML, conceptual model of the UML, Architecture.
Basic Structural Modeling: Classes, Relationships, common Mechanisms, and diagrams. Class
&Object Diagrams: Terms, concepts, modelling techniques for Class & Object Diagrams
Collaboration Diagrams: Terms, Concepts, depicting a message, polymorphism in collaboration
Diagrams, iterated ‘messages, use of self in messages. Sequence Diagrams: Terms, concepts,
AL_| depicting asynchronous messages with/without priority, callback mechanism, broadcast messages
Basic Behavioural Modeling: Use cases, Use case Diagrams, Activity Diagrams, State Machine ,
Process and thread, Event and signals, Time diagram, interaction diagram, Package diagram.
Architectural Modeling: Component, Deployment, Component diagrams and Deployment
diagrams.
Object Oriented Analysis: Object oriented design, Object design, Combining three models, De:
algorithms, design optimization, Implementation of control, Adjustment of inheritance,
representation, Physical packaging, Documenting design considerations
Structured analysis and structured design (SA/SD), Jackson Structured Development
TH | (ISD) Mapping object oriented concepts using non-object oriented language, Translating classes into
data structures, Passing arguments to methods, Implementing inheritance, associations encapsulation,
Object oriented programming style: reusability, extensibility, robustness, programming in the
large. Procedural v/s OOP, Object oriented language features. Abstraction and Encapsulation,
Obj
GH Basics = Overview, Program situcture, namespac
operators, typecasting, control structures
TV | C++ Functions : Simple functions, Call and Return by reference,
functions, Overloading of functions, default a
, identifiers, variables, constants, enum,
nline functions, Macro Vs. Inline
nd functions, virtual functions
-guments, fri
Objects and
lasses : Basics of object and class in C++, Private and public members, static daia
and function members, constructors and their types, destructors, operator overloading, type
y_| conversion. Inheritance : Concept of Inheritance, types of inherita
hierarchical, hybrid, protected members, overriding, virtual base class
Polymorphi in C++, Pointes and Objects, this pointer, virtual and pure virtual
functions, Implementing polymorphism
: single, multiple, multilevel,
m = PointeEngineering Express
Basics of object and class in C++
OBJECT
__||Let’s see an example to create object of student class using sl as the ||.
In C++, Object isa real-world entity, for example, chair, car, pen, |
|mobile, laptop etc. pe
in they binds: on oljectokeran rondo dhatshos ‘inte’ and behaviors — || E
|Here, state means data and behavior means functionality. a
pie : : Bh 5 . [
| The object is an instance of a class. All the members of the class a
can be accessed through object. L
reference varie L
Student sl; //creating an object of Student L
|| members and member functions define the properties and behavior of |
CLASS. L
|Class_is like a blueprint for.an object. dt
|/t_is a user-defined data type that_has data members and member ||
| functions.
| Data members are the data variables and member functions are the |
| functions used to manipulate these variables together, these data __|
class Class_Name
access_specifier:
Here, ‘cess specifier defines the le f access to the class’:
data members,
// Body of the class
BEngineering Express
Example-
class ThisClass
{
public:
int var; // data member
void print() / member function
f
cout << "Hello";
/L_C++ program to illustrate how to create o simple class and object
#inchude
include
using namespace std;
Define a class named ‘Person’
class Person {
public:
Data members
string name;
int age;
Member function to introduce the person
void introduce()
{
cout << "Hi, my name is "<< name <<" and fam“
<< age <<" years old." << endl;
}
h
int main()
t
Create an object of the Person class,
Person person;
accessing data members
person .name = "Alice",
personI age = 30;
/ Call the introduce member method
person| introduce();
getch();
retum 0; Ouapat
) Hi, my name is Alice and I am 30 years old,Engineering Express
Access Modifiers
lin C++ classes, we can control the access to the members of the |
|the class under that access specifier will have access level. __|
; specif if [|
LT
lin C++, there are 3 access specifiers that are as follows:
_|| Public: Members declared as public can be accessed from outside the cla:
| Private: Members declared as private can only be accessed within the
|class itself.
| Protected: Members declared as protected can be accessed within the
class and by derived classe:
f if he ‘i ifier i
lapplied to every member by default ___________
[//C+- program to demonstrate accessing of data member
include
‘include (1);// Cast double to int
width — static cast
using namespace std;
class Test {
—_]}} pubic:
1) User-Defined Constructor
——] _ Testd) { cout <<"\n Constructor executed”; }
1 User-Defined Destructor
~Test() { cout << "nDestructor executed”; }
— I}
‘mt main()
t
mnt Ouip
7 Constructor executed
,
Destructor executed___ Engineering Express
Output
Default constructor called!
Length: 0, Width: 0
Parameterized constructor called!
Length: 10, Width: $
Overloaded constructor (with double parameters) called!
Length: 15, Width: 7
Copy constructor called!
Length: 10, Width: 5
Destructor LL
A destructor is a special member function of a class by the same L
[name as class name which is executed automatically whenever an L
object of a class is destroyed(deleted). L
|A destructor is defined to free up the resource that_are no eer | g
Lrequired. Lo
_||class name as follows:
To ad g F (x) i [
~class name() L
£
eVDDECS |
ae i ay
Wl and destructor
——} Tests;
pb
‘include
using namespace std;
class Test {
1/ User-Defined Constructor
Test() { cout <<"\n Constructor executed"; }
1 User-Defined Destructor
~Tesi() { cout << "\nDestructor executed"; }
int mang
{
‘Output
Constructor executed
return 0;
Destructor executed
iLEngineering Express
following :
Scope Resolution Operator C +: )
| Class Member Access Operators C.,)
Sizeof Operator
Temary Operator C2: ) pp
Defining Operator Overloading
| To overload an operator in C++, We are required to define a special |
|function called Operator Function =
| Operator functions are the same_as normal functions. The only ____|
\differences are, that the name of an operator function is always the |
| operator keyword followed by the symbol_of the operator,.and operator |
| functions are called when the corresponding operator is used. _____|
: Carglist)
JN
11 body
2
| Type of Operators
Unary Operators
Binary Operotors| Engineering Express
erloading Unary Operato
void operator symbol(void)
fe
1 body
L
___||As we already know, binary operators are the operators that are used |
____||to operate two operands. Therefore, whenever defining an operator |
___|largument because the reference of one argument will.automatically be |
; oi :
[function to overload binary operator, we are required to pass only one |
|passed to which the operator function will be called. _____|Engineering Express
|// Show an example which contain both unary operator and binary }|
| /doperator. [
include
using namespace std; fi
class Point {
private:
int x, y5
public:
#/ Constructor to initialize the point
Point(int x ~ 0, int y = 0) x(x), y(y)
// Display funetion to print the point
void display() const {
cout << "(" cox <<", "<
using namespace std;
int main() {
inta=10; — // int type
floatb=a; — // Implicit conversion from int to float
cout << "Integer a: "<< a << endl;
cout << "Float b: "<< b << endl;
return 0;
Output
Integer a: 10
Float b: 10Engineering Express
Pri ie Gain OI =
; a iS ah
‘
| In_such cases, you use explicit type conversion using a cast. ____|
[7 ee
+#include
using namespace std;
int main() {
double x= 9.99; // double type
int y = (int)x; _// Explicit conversion from double to int
cout << "Double x: " <
using namespace std;
i/ base class
class Vehicle {
public:
Vehicle() { cout << "This is a Vehicle\n"; }
B
// sub class derived from a single base classes.
class Car : public Vehicle {
public
Car() { cout << "This Vehicle is Carin"; }
hy
// main function
‘int main()
t
/] Creating object of sub class will
invoke the constructor of base classes
tran Output
return 0;
This is a Vehicle
: This Vehicle is CarEngineering Express
==
| Multiple Inheritance
| Multiple Inheritance is a feature of C++ where a_class can inherit
from more than one class. ie one subclass is inherited from moreEngineering Express
= ae
#include
using namespace std;
// first base class
class Vehicle {
public:
Vehicle() { cout << "This is a Vehicle\n"; }
hs
// second base class
class FourWheeler {
public:
FourWheeler() { cout << "This is a 4 Wheeler\n"; }
hs
// sub class derived from two base classes
class Car : public Vehicle, public FourWheeler {
public:
Car() { cout << "This 4 Wheeler Vehical is a Car\n"; }
hs
// main function
int main()
t
// Creating object of sub class will
// invoke the constructor of base classes.
Car obj;
turn 0;
aes Output
This is a Vehicle
}
This is a 4 Wheeler
This 4 Wheeler Vehical is a CarEngineering Express
| Syntax
£
f
le i le : ifier_deri\
£
(a
class C.
t
i
class B’: public C — ________—
x xEngineering [xpress
|\// C++ program to implement Multilevel Inheritance +t
#include
using namespace std;
// base class
class Vehicle {
public:
Vehicle() { cout << "This is a Vehicle\n"; }
1
// first sub_class derived from class vehicle
class fourWheeler : public Vehicle {
public:
fourWheeler() { cout << "4 Wheeler Vehicles\n"; }
hs
// sub class derived from the derived base class fourWheeler
class Car : public fourWheeler {
public:
Car() { cout << "This 4 Wheeler Vehical is a Car\n"; }
hb
// main function
int main()
t
// Creating object of sub class will
// invoke the constructor of base classes.
Car obj;
return 0;
Output
This is a Vehicle
4 Wheeler Vehical
This 4 Wheeler Vehical is a CarEngineering Express
; 5
£
i
ri I I
iz
i
class A
5
body of the class A
3
wi _class B: public A at
£
AS body of class B. LE
a
class C : public A
£
LL body of class C.
class D_: public A
body of class D.Engineering Express
#include
using namespace std;
// base class
class Vehicle {
public:
Vehicle() { cout << "This is a Vehicle\n"; }
hs
// first sub class
class Car : public Vehicle {
public:
Car() { cout << "This Vehicle is Car\n"; }
Hs
// second sub class
class Bus : public Vehicle {
public:
Bus() { cout <<"This Vehicle is Bus\n"; }
h
// main function
int main()
t
// Creating object of sub class will
// invoke the constructor of base class.
Car obj;
Bus obj2;
ae P ‘ . .
return 0; Output
This is a Vehicle
This Vehicle is Car
This is a Vehicle
This Vehicle is BusEngineering Express
|Hybrid_Inheritance
| Hybrid Inheritance is implemented by combining more than one type |
lof inheritance. For example: Combining Hierarchical inheritance and |
i 2 P ep aes agree
5 a
class A: public BE
2
class C ; public BE
2Engineering Express
a . ion of Hybri A
#include
using namespace std;
// base class
class Vehicle {
public:
Vehicle() { cout << "This is a Vehicle\n"; }
1
h
// base class
class Fare {
public:
Fare() { cout << "Fare of Vehicle\n"; }
i
!/ first sub class
class Car : public Vehicle {
public:
Car() { cout <<"This Vehical is a Car\n"; }
hi
// second sub class
class Bus : public Vehicle, public Fare {
public:
Bus() { cout << "This Vehicle is a Bus with Fare\n"; }
4
// main function
int main()
{
// Creating object of sub class will
//invoke the constructor of base class,
Bus obj2;
return 0;
} Output
This is a Vehicle
Fare of Vehicle
This Vehicle is a Bus with Fare| Engineering Express |