0% found this document useful (0 votes)
10 views15 pages

OOPS For Design Pattern

The document discusses constructors in object-oriented programming, particularly in C++, detailing their purpose, types (default, parameterized, copy, and move), and characteristics. It also covers the relationships in software design, such as inheritance, association, composition, and aggregation, along with use case relationships in system design. Understanding these concepts is essential for creating maintainable and scalable systems.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views15 pages

OOPS For Design Pattern

The document discusses constructors in object-oriented programming, particularly in C++, detailing their purpose, types (default, parameterized, copy, and move), and characteristics. It also covers the relationships in software design, such as inheritance, association, composition, and aggregation, along with use case relationships in system design. Understanding these concepts is essential for creating maintainable and scalable systems.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

OOPS for Design Pattern

In object-oriented programming, a constructor is a special method used to


initialize an object when it's created, ensuring it starts in a valid state.
 Purpose: Constructors are crucial for setting initial values for an object's
attributes (data members) and establishing the object's invariant (the conditions
that must be true for the object to be in a valid state).
 Automatic Invocation: When a new object of a class is created, the constructor
is automatically called.
 No Return Type: Constructors, unlike regular methods, do not have a return
type, including void.
 Name Convention: Constructors typically have the same name as the class they
belong to.
 Overloading: Classes can have multiple constructors that take different
arguments, allowing for flexible initialization.
Types of Constructors:
o Default Constructor: A constructor that takes no arguments.
o Parameterized Constructor: A constructor that takes arguments, allowing for
initialization with specific values.
o Copy Constructor: A constructor that initializes an object using another object
of the same class.
o Private Constructor: A constructor that is restricted to be called only from
within the class.
o Static Constructor: A constructor that is used to initialize static members of a
class.


 Example:

#include <iostream>
using namespace std;

class A {
public:
// Constructor of the class without
// any parameters
A() {
cout << "Constructor called" << endl;
}
};

int main() {
A obj1;
return 0;
}

Output
Constructor called
Explanation: In this code, we defined a constructor for class A. In main
function, when we create an object of that class, this constructor is called.
Types of Constructors in C++
Constructors can be classified based on in which situations they are being used.
There are 4 types of constructors in C++:
1. Default Constructor
2. Parameterized Constructor
3. Copy Constructor
4. Move Constructor
1. Default Constructor
A default constructor is a constructor that doesn’t take any argument. It has no
parameters. It is also called a zero-argument constructor. The compiler
automatically creates an implicit default constructor if the programmer does not
define one.
Example:
#include <iostream>
using namespace std;

// Class with no explicity defined constructors


class A {
public:
};

int main() {

// Creating object without any parameter


A a;
cout << "In Main";
return 0;
}

Output
In Main
The object of the class A is created without any parameters, hence the default
constructor exists.

2. Parameterized Constructor
Parameterized constructors make it possible to pass arguments to constructors.
Typically, these arguments help initialize an object when it is created. To create
a parameterized constructor, simply add parameters to it the way you would to
any other function. When you define the constructor’s body, use the parameters
to initialize the object.
Example:
#include <iostream>
using namespace std;

class A {
public:
int val;

// Parameterized Constructor
A(int x) {
val = x;
}
};

int main() {

// Creating object with a parameter


A a(10);
cout << a.val;
return 0;
}

Output
10
In this code, the parameterized constructor is called when we create
object a with integer argument 10 . As defined, it initializes the member
variable val with the value 10.
Note: If a parameterized constructor is defined, the non-parameterized
constructor should also be defined as compiler does not create the default
constructor.
3. Copy Constructor
A copy constructor is a member function that initializes an object using another
object of the same class. Copy constructor takes a reference to an object of the
same class as an argument.
Example:
#include <iostream>
using namespace std;

class A {
public:
int val;

// Parameterized constructor
A(int x) {
val = x;
}

// Copy constructor
A(A& a) {
val = a.val;
}
};

int main() {
A a1(10);

// Creating another object from a1


A a2(a1);

cout << a2.val;


return 0;
}

Output
10
In this example, the copy constructor is used to create a new object a2 as a copy
of the object a1. It is called automatically when the object of class A is passed
as constructor argument.
Just like the default constructor, the C++ compiler also provides an implicit
copy constructor if the explicit copy constructor definition is not present.
Here, it is to be noted that, unlike the default constructor where the presence of
any type of explicit constructor results in the deletion of the implicit default
constructor, the implicit copy constructor will always be created by the compiler
if there is no explicit copy constructor or explicit move constructor is present.
4. Move Constructor
The move constructor is a recent addition to the family of constructors in C++.
It is like a copy constructor that constructs the object from the already existing
objects., but instead of copying the object in the new memory, it makes use of
move semantics to transfer the ownership of the already created object to the
new object without creating extra copies.
It can be seen as stealing the resources from other objects.
Syntax:
className (className&& obj) {
// body of the constructor
}
The move constructor takes the rvalue reference of the object of the same class
and transfers the ownership of this object to the newly created object.
Like a copy constructor, the compiler will create a move constructor for each
class that does not have any explicit move constructor.

When are Constructors Called?

When are the constructors called for different types of objects like global, local,
static local, dynamic?
1) Global objects: For a global object, constructor is called before main() is
called. For example, see the following program and output:

#include<iostream>
using namespace std;

class Test
{
public:
Test();
};

Test::Test() {
cout << "Constructor Called \n";
}

Test t1;

int main() {
cout << "main() started\n";
return 0;
}
/* OUTPUT:
Constructor Called
main() started
*/
2) Function or Block Scope ( automatic variables and constants ) For a non-
static local object, constructor is called when execution reaches point where
object is declared. For example, see the following program and output:

using namespace std;

class Test
{
public:
Test();
};

Test::Test() {
cout << "Constructor Called \n";
}

void fun() {
Test t1;
}

int main() {
cout << "Before fun() called\n";
fun();
cout << "After fun() called\n";
return 0;
}
/* OUTPUT:
Before fun() called
Constructor Called
After fun() called
*/
For a local static object, the first time (and only the first time) execution
reaches point where object is declared. For example, output of the following
program is:

#include<iostream>
using namespace std;

class Test
{
public:
Test();
};

Test::Test() {
cout << "Constructor Called \n";
}

void fun() {
static Test t1;
}

int main() {
cout << "Before fun() called\n";
fun();
cout << "After fun() called\n";
fun(); //constructor is not called this time.
return 0;
}
/* OUTPUT
Before fun() called
Constructor Called
After fun() called
*/
3) Class Scope: When an object is created, compiler makes sure that
constructors for all of its subobjects (its member and inherited objects) are
called. If members have default constructors or constructor without parameter
then these constructors are called automatically, otherwise parameterized
constructors can be called using Initializer List. For example, see PROGRAM 1
and PROGRAM 2 and their output.

// PROGRAM 1: Constructor without any parameter


#include<iostream>
using namespace std;

class A
{
public:
A();
};

A::A() {
cout << "A's Constructor Called \n";
}

class B
{
A t1;
public:
B();
};

B::B() {
cout << "B's Constructor Called \n";
}

int main() {
B b;
return 0;
}
/* OUTPUT:
A's Constructor Called
B's Constructor Called
*/

// PROGRAM 2: Constructor with parameter (using initializer list)


#include <iostream>
using namespace std;

class A
{
public:
int i;
A(int );
};

A::A(int arg)
{
i = arg;
cout << "A's Constructor called: Value of i: " << i << endl;
}

// Class B contains object of A


class B
{
A a;
public:
B(int );
};

B::B(int x):a(x)
{
cout << "B's Constructor called";
}

int main()
{
B obj(10);
return 0;
}
/* OUTPUT
A's Constructor called: Value of i: 10
B's Constructor called
*/
4) Dynamic objects: For a dynamically allocated object, constructor is invoked
by new operator. For example, see the following program and output.

#include<iostream>

using namespace std;

class Test
{
public:
Test();
};

Test::Test() {
cout << "Constructor Called \n";
}

int main()
{
cout << "Before new called\n";
Test *t1 = new Test;
cout << "After new called\n";
return 0;
}
/* OUTPUT
Before new called
Constructor Called
After new called
*/

Characteristics of Constructors
 The name of the constructor is the same as its class name.
 Constructors are mostly declared as public member of the class though they
can be declared as private.
 Constructors do not return values, hence they do not have a return type.
 A constructor gets called automatically when we create the object of the
class.
 Multiple constructors can be declared in a single class (it is even
recommended – Rule Of Three, The Rule of Five).
 In case of multiple constructors, the one with matching function signature
will be called.

software design, relationships


In software design, relationships between components or entities are crucial for
creating maintainable and scalable systems. Key relationships
include inheritance (IS-A), association (HAS-A), composition (part-of), and
aggregation, which are fundamental to object-oriented programming (OOP).
Here's a more detailed breakdown:
1. Object-Oriented Relationships:
Inheritance (IS-A):
A class inherits properties and behaviors from a parent class, establishing an
"is-a" relationship (e.g., a Dog is a Animal).
Association (HAS-A):
One class has a relationship with another, indicating that it "has" or uses
another class (e.g., a Car has an Engine).
Composition (part-of):
A class is composed of other classes, meaning the other classes are part of its
structure and are tightly coupled (e.g., a House is composed
of Walls, Windows, Doors).
Aggregation:
Similar to composition, but the relationship is weaker, meaning the
components can exist independently (e.g.,
a University aggregates Departments, but the departments can exist
independently).
2. Other Important Relationships:
Dependency:
One class depends on another for its functionality, but the dependency is not as
strong as composition or aggregation.
Realization:
One thing specifies the behavior or responsibility to be carried out, and the
other thing carries out that behavior. Tpoint Tech explains
Entity-Relationship Diagrams (ERDs):
Visual representations of the structure of a database, showing entities and their
relationships, which is crucial for database design.
Software Architecture Patterns:
Reusable, proven solutions to recurring problems at the system level,
addressing concerns related to the overall structure, component interactions,
and quality attributes of the system.
Use Case Relationships:
Generalization, association, and dependency relationships between use cases.

Use Case Relationship Types:


Introduction:
In the realm of system design and software development, creating a
comprehensive blueprint is essential to ensure the seamless functionality of
complex applications. Use cases, pivotal in this process, serve as building
blocks that define user-system interactions. However, understanding not only
what use cases are but also how they interconnect is equally critical. This article
delves into the intricate relationships that weave the fabric of use cases,
shedding light on their purpose, types, and practical implications.
The Relationship Types of Use Cases
Use cases, in the context of Unified Modeling Language (UML), can be
interconnected using various relationships or connectors to represent the flow of
interactions and dependencies between them. These relationships help provide a
more comprehensive understanding of how different use cases within a system
or software application are related and how they collaborate. Let’s explore some
of the common types of relationships among use cases:
1. Association Relationship: An association relationship is used to show that
two or more use cases are related or associated with each other in some way.
This relationship doesn’t specify the direction of the interaction but indicates
a general association. For example, if two use cases often occur together or
share some common elements, you can represent this using an association
relationship.
2. Include Relationship: The include relationship indicates that one use case
includes another use case. This means that the included use case is a part of
the main use case and is essential for its execution. The inclusion
relationship is often used to represent shared or reusable functionality. For
instance, a “Make Payment” use case might include an “Authenticate User”
use case.
3. Extend Relationship: The extend relationship represents optional or
conditional behavior that can extend the functionality of a base use case
under specific conditions. It indicates that an extending use case can add
extra behavior to the base use case if certain conditions are met. For
example, an “Order Processing” use case might be extended by an “Apply
Discount” use case if the user is eligible for a discount.
4. Generalization Relationship: In UML, generalization represents
inheritance. When one use case generalizes another, it means that the
generalized use case serves as a superclass, and the generalizing use case is a
subclass that inherits its behavior. This relationship is often used to show
how a more specific use case inherits characteristics from a more general
one
5. Dependency Relationship: Dependency relationships between use cases
indicate that one use case relies on another, but it’s not necessarily a direct
association or inclusion. It signifies that a change in one use case may affect
another. Dependencies can be used to represent indirect relationships and
can be valuable for managing change impact.
Understanding and effectively using these relationships among use cases is
crucial for modeling complex systems and applications accurately. These
relationships help project teams and stakeholders visualize how different parts
of the system interact, collaborate, and depend on each other, contributing to a
better overall understanding of system behavior and architecture. Here’s a
summary table of the common types of relationships among use cases, including
when to use them:

Relationship
Type Description When to Use

Indicates a general When two or more use cases


association between use are loosely related or
Association cases. associated.

When one use case is


Specifies that one use essential for the execution of
Include case includes another. another.

Represents optional or When a use case may extend


Extend conditional behavior. the functionality of another.

When a specific use case


Indicates inheritance inherits behavior from a
Generalization between use cases. general one.
Relationship
Type Description When to Use

Shows reliance between When one use case depends


Dependency use cases. on another indirectly.

You might also like