OOPs ASSIGNMENT
OOPs ASSIGNMENT
OOPs ASSIGNMENT
4) Adding new data and functions is not 4) Adding new data and function is
easy. easy.
12) Code reusability absent in procedural 12) Code reusability present in object-
programming, oriented programming.
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;
}
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
};
int main ()
{
Person obj1("First person",25);
obj1.display();
Person obj2(obj1);
obj2.display();
return 0;
}
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;
}
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;
}
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.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;
}