Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

OOPs ASSIGNMENT

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 18

OOPs ASSIGNMENT – 1

Q.1 Explain various features of object oriented programming.


Compare the object oriented system with procedure oriented
system.
Ans. Features of oops –
1) Programs are divided into entities known as object.
2) Data is hidden and cannot be accessed by external function.
3) Object communicate with each other using functions.
4) New data and functions easily added whenever needed.
5) Follow bottom-up approach in program design.

 Difference between POP and OOP –

Procedural Oriented Programming Object-Oriented Programming

1) In procedural programming, the 1) In object-oriented programming, the


program is divided into small parts program is divided into small parts
called functions. called objects.

2) Procedural programming follows 2) Object-oriented programming


a top-down approach. follows a bottom-up approach.

3) Object-oriented programming has


3) There is no access specifier in
access specifiers like private, public,
procedural programming.
protected, etc.

4) Adding new data and functions is not 4) Adding new data and function is
easy. easy.

5)Procedural programming does not have 5) Object-oriented programming


any proper way of hiding data so it is less provides data hiding so it is more
secure. secure.

6)In procedural programming, 6) Overloading is possible in object-


overloading is not possible. oriented programming.
Procedural Oriented Programming Object-Oriented Programming

7) In object-oriented programming, the


7)In procedural programming, there is no
concept of data hiding and inheritance is
concept of data hiding and inheritance.
used.

8) In procedural programming, the 8) In object-oriented programming, data


function is more important than the data. is more important than function.

9) Procedural programming is based on 9) Object-oriented programming is


the unreal world. based on the real world.

10) Object-oriented programming is


10) Procedural programming is used for
used for designing large and complex
designing medium-sized programs.
programs.

11) Procedural programming uses the 11) Object-oriented programming uses


concept of procedure abstraction. the concept of data abstraction.

12) Code reusability absent in procedural 12) Code reusability present in object-
programming, oriented programming.

Examples: C, FORTRAN, Pascal, Basic,


Examples: C++, Java, Python, C#, etc.
etc.

Q.2 Explain Object Oriented Concept in detsils also explain


applications of OOP’s ?

Ans. Object-oriented programming – As the name suggests uses


objects in programming. Object-oriented programming aims to
implement real-world entities like inheritance, hiding,
polymorphism, etc. in programming. The main aim of OOP is to
bind together the data and the functions that operate on them so
that no other part of the code can access this data except that
function.
There are some basic concepts that act as the building blocks of
OOPs i.e. =
1) Class
2) Objects
3) Encapsulation
4) Abstraction
5) Polymorphism
6) Inheritance
7) Dynamic Binding
8) Message Passing

Class -
The building block of C++ that leads to Object-Oriented programming
is a Class. It is a user-defined data type, which holds its own data
members and member functions, which can be accessed and used by
creating an instance of that class. A class is like a blueprint for an
object. A Class 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 members and member functions define
the properties and behaviour of the objects in a Class. In the example
of class Car, the data member will be speed limit, mileage, etc and
member functions can apply brakes, increase speed, etc.

Objects -
An Object is an identifiable entity with some characteristics and
behaviour. An Object is an instance of a Class. When a class is
defined, no memory is allocated but when it is instantiated (i.e. an
object is created) memory is allocated. Objects take up space in
memory and have an associated address like a record in pascal or
structure or union. When a program is executed the objects interact
by sending messages to one another. Each object contains data and
code to manipulate the data. Objects can interact without having to
know details of each other’s data or code, it is sufficient to know the
type of message accepted and the type of response returned by the
objects.

Encapsulation -
In normal terms, Encapsulation is defined as wrapping up data and
information under a single unit. In Object-Oriented Programming,
Encapsulation is defined as binding together the data and the
functions that manipulate them. Encapsulation also leads to data
abstraction or data hiding. Using encapsulation also hides the data. In
the above example, the data of any of the sections like sales, finance,
or accounts are hidden from any other section.

Abstraction -
Data abstraction is one of the most essential and important features
of object-oriented programming in C++. Abstraction means displaying
only essential information and hiding the details. Data abstraction
refers to providing only essential information about the data to the
outside world, hiding the background details or implementation.

Polymorphism -
The word polymorphism means having many forms. In simple words,
we can define polymorphism as the ability of a message to be
displayed in more than one form. A person at the same time can
have different characteristics. A man at the same time is a father, a
husband, and an employee. So the same person possesses different
behaviour in different situations. This is called polymorphism. An
operation may exhibit different behaviours in different instances. The
behaviour depends upon the types of data used in the operation. C++
supports operator overloading and function overloading.
1) Operator Overloading: The process of making an operator
exhibit different behaviours in different instances is known as
operator overloading.
2) Function Overloading: Function overloading is using a single
function name to perform different types of tasks.
Polymorphism is extensively used in implementing inheritance.

Inheritance -
The capability of a class to derive properties and characteristics from
another class is called Inheritance. Inheritance is one of the most
important features of Object-Oriented Programming.

Sub Class: The class that inherits properties from another class is
called Sub class or Derived Class.
Super Class: The class whose properties are inherited by a sub-class is
called Base Class or Superclass.
Reusability: Inheritance supports the concept of “reusability”, i.e.
when we want to create a new class and there is already a class that
includes some of the code that we want, we can derive our new class
from the existing class. By doing this, we are reusing the fields and
methods of the existing class.

Dynamic Binding -
In dynamic binding, the code to be executed in response to the
function call is decided at runtime. C++ has virtual functions to
support this. Because dynamic binding is flexible, it avoids the
drawbacks of static binding, which connected the function call and
definition at build time.
Message Passing -
Objects communicate with one another by sending and receiving
information. A message for an object is a request for the execution of
a procedure and therefore will invoke a function in the receiving
object that generates the desired results. Message passing involves
specifying the name of the object, the name of the function, and the
information to be sent.

Q.3 Write a program in C++ print customer data using class & object
concept?
Ans.
#include <iostream>
#include <string>
using namespace std;
class Customer {
private:
string name;
string address;
string phoneNumber;
int customerId;

public:
Customer(std::string n, std::string addr, std::string phone, int id) {
name = n;
address = addr;
phoneNumber = phone;
customerId = id;
}
void displayCustomerData() {
cout << "Customer ID: " << customerId << endl;
cout << "Name: " << name << endl;
cout << "Address: " << address << endl;
cout << "Phone Number: " << phoneNumber << endl;
}
};

int main() {
Customer customer1("Rahul Sharma", "123 Main St", "555-1234",
1001);
cout << "Customer Data:" << endl;
customer1.displayCustomerData();
return 0;
}

Q.4 What is constructor? Mention its type. Explain different types


of constructor with an example?

Ans.
Constructor –
A constructor is a special member function of a class and shares the
same name as of class, which means the constructor and class have
the same name. Constructor is called by the compiler whenever the
object of the class is created, it allocates the memory to the object
and initializes class data members by default values or values passed
by the user while creating an object. Constructors don’t have any
return type because their work is to just create and initialize an
object.
There are three types of constructors in c++ -
1) Default constructor
2) Parameterized constructor
3) Copy Constructor

(i) Default Constructor -


Default constructor is also known as a zero-argument constructor, as
it doesn’t take any parameter. It can be defined by the user if not
then the compiler creates it on his own. Default constructor always
initializes data members of the class with the same value they were
defined.
Example –
#include <iostream>
using namespace std;
class Person {
private:
string name;
int age;
public:
Person()
{
cout<<"Default constructor is called"<<endl;
name = "student";
age = 12;
}
void display()
{
cout<<"Name of current object: "<<name<<endl;
cout<<"Age of current object: "<<age<<endl;
}
};
int main ()
{
Person obj;
obj.display();
return 0;
}

(ii) Parameterized Constructor –


Parameterized constructor is used to initialize data members with the
values provided by the user. This constructor is basically the
upgraded version of the default constructor. We can define more
than one parameterized constructor according to the need of the
user, but we have to follow the rules of the function overloading, like
a different set of arguments must be there for each constructor.
Example –
#include <iostream>
using namespace std;
class Person {
private:
string name;
int age;
public:
Person(string person_name, int person_age)
{
cout<<"Constructor for name and age is called"<<endl;
name = person_name;
age = person_age;
}
void display()
{
cout<<"Name of object: "<<name<<endl;
cout<<"Age of current object: "<<age<<endl;
cout<<endl;
} } ;
int main()
{
Person obj ("Rahul",15);
obj.display();
return 0;
}

(iii) Copy Constructor -


If we have an object of a class and we want to create its copy in a
new declared object of the same class, then a copy constructor is
used. The compiler provides each class a default copy constructor
and users can define it also. It takes a single argument which is an
object of the same class.
Example –
#include <iostream>
using namespace std;
class Person {
private:
string name;
int age;
public:
Person (string person_name, int person_age)
{
cout<<"Constructor for both name and age is called"<<endl;
name = person_name;
age = person_age;
}

Person(const Person& obj)


{
cout<<"Copy constructor is called"<<endl;
name = obj.name;
age = obj.age;
}
void display()
{
cout<<"Name of current object: "<<name<<endl;
cout<<"Age of current object: "<<age<<endl;
cout<<endl;
}

};
int main ()
{
Person obj1("First person",25);
obj1.display();
Person obj2(obj1);
obj2.display();
return 0;
}

Q.5 Write a program in C++ Print student data using array of


structure?

Ans.
#include <iostream>
#include <string>
using namespace std;
struct Student {
string name;
int rollNumber;
float marks;
};

int main() {
const int numStudents = 3;
Student students[numStudents];
for (int i = 0; i < numStudents; i++) {
cout << "Enter details for student " << i + 1 << ":\n";
cout << "Name: ";
getline(cin , students[i].name);
cout << "Roll Number: ";
cin >> students[i].rollNumber;
cout << "Marks: ";
cin >> students[i].marks;
cout << endl;
}
cout << "Student Data:\n";
for (int i = 0; i < numStudents ; i++) {
cout << "Student " << i + 1 << endl ;
cout << "Name: " << students[i].name << endl;
cout << "Roll Number: " << students[i].rollNumber << endl;
cout << "Marks: " << students[i].marks << endl << endl;
}

return 0;
}

Q.6 What is Friend Function, Explain with an Example?

Ans.
Friend Function –
A friend function is a function that is not a member of a class but is
granted access to its private and protected members. A friend
function is declared with the friend keyword inside the class, but it is
defined outside the class. This provides the function with the ability
to access private and protected members of the class.
Example –
#include <iostream>
using namespace std;
class MyClass;
void showPrivateData (const MyClass& obj);
class MyClass {
private:
int privateData;
public:
MyClass (int data): privateData(data) {}
friend void showPrivateData (const MyClass& obj);
};
void showPrivateData (const MyClass& obj) {
cout << "Private Data: " << obj.privateData << endl;
}
int main () {
MyClass myObject (42);
showPrivateData(myObject);
return 0;
}

Q.7 What is dynamic memory allocation? How it’s is done in c++?


Explain with example?
Ans.
Dynamic Memory Allocation –
Dynamic memory allocation in C++ refers to the process of allocating
memory at runtime, allowing the program to request memory
dynamically and manage it as needed. This is in contrast to static
memory allocation, where memory is allocated at compile-time and
is fixed during the program's execution.
In C++, dynamic memory allocation is achieved using two operators:
new and delete (or delete [] for arrays).

new Operator:
The new operator is used to allocate memory for a single variable or
an array dynamically. It returns a pointer to the allocated memory.
delete [] Operator:
The delete operator is used to deallocate memory that was allocated
using new, and the delete [] operator is used to deallocate memory
that was allocated using new [].
Example –
#include <iostream>
using namespace std;
int main () {
int* dynamicIntArray = new int [5];
for (int i = 0; i < 5; ++i) {
dynamicIntArray[i] = i * 10;
}
for (int i = 0; i < 5; ++i) {
cout << "Element " << i << ": " << dynamicIntArray[i] << endl;
}
delete [] dynamicIntArray;
return 0;
}

Q.8 Explain inline Function with an example?


Ans.
Inline Function –
The inline keyword is used to suggest the compiler that a function
should be expanded in place (i.e., replace the function call with the
actual code) rather than performing a regular function call. This can
lead to potentially better performance by avoiding the overhead of a
function call. It is a request to the compiler, and the compiler may
choose to ignore it.
Example –
#include <iostream>
using namespace std;
inline int square (int x) {
return x * x;
}
int main () {
int num = 5;
int result = square(num);
scout << "Square of " << num << " is: " << result << endl;
return 0;
}

Q.9 Write a program in C++ to show the concept of function


overloading?
Ans.
#include <iostream>
using namespace std;
int add (int a, int b) {
return a + b;
}
int add (int a, int b, int c) {
return a + b + c;
}
double add (double a, double b) {
return a + b;
}
int main () {
cout << "Sum of two integers: " << add (3, 5) << endl;
cout << "Sum of three integers: " << add (3, 5, 7) << endl;
cout << "Sum of two doubles: " << add (3.5, 4.2) << endl;
return 0;
}

Q.10 How member functions are defined inside the class or outside
the class, Explain with an example?
Ans. Member functions of a class can be defined either inside the
class declaration (inline definition) or outside the class declaration.
Here's an example illustrating both cases:
#include <iostream>
using namespace std;
class MyClass {
private:
int data;
public:
void setDataInline (int value) {
data = value;
}
void displayDataOutside ();
void setDataOutside (int value);
};
void MyClass :: displayDataOutside() {
cout << "Data: " << data << endl;
}
void MyClass::setDataOutside(int value) {
data = value;
}

int main () {
MyClass obj;
obj.setDataInline(42);
obj.displayDataOutside();
obj.setDataOutside(99);
obj.displayDataOutside();
return 0;
}

You might also like