OOPS Assignment 2
OOPS Assignment 2
OOPS Assignment 2
OBJECT ORIENTED
PROGRAMMING
ASSIGNMENT - 2
Submitted by -
Siddharth Mittal
2K22/SE/172
Q1)List the different operators in C++ with code snippets demonstrating their functionality
1) Input-Output Operators
<< (Insertion or put to) - Used for output, typically to display data on the console or
write it to a file. It is often used with the cout stream.
>> (Extraction or get from) - Used for input to read data from the console or from a
file into a variable. It is commonly used with the cin stream.
2) Reference Operator
& - Used to create another reference identifier to an existing variable.
7) Miscellaneous Operators
sizeof) - Used to determine the size (in bytes) of a datatype or an object.
CODES -
int n;
cout<<'enter a number:":
cin>>n;
cout<<"'number is "<<n<<endl:
return 0;
#include <iostream>
using namespace std;
int main)
int x = 42:
cout<<"x="<<x<<endl;
x
int& y X;I/ yis areference tovalue
modifies the of x to 55
y=55; I/ This
cout<<"x="<<x<Kendl:
return 0;
/Demonstrating class member access operators
lland member dereferencing operators
#include <iostream>
using namespace std;
int main(0
class Demo
public:
int x;
Demo obj;
obj.x =42; l/Accessing class member using'"
cout<<"x="<<x<Kendl:
obj_ptr-&obj;
obj_ptr->x-30; //Accessing class member using '->'
cout<<"x="<<x<<endl;
int Demo:* mem ptr-&Demo::x; /Using ':* tocreate a pointer to a class member
obj.*mem ptr-69; lAccessing class member using':
cout<<"x="<<x<<endl;
int globalVar=10;
|/Outer class
class Outer
public:
static int staticVar; // Static variable inside Outer class
1/ Nested class
class Inner
public:
static int innerVar; // Staticvariable inside Inner class
|/ Member function of lnner class
void displaylnnerVar()
cout<<"nner::innerVar: "<<innerVar<<<endl;
int main()
int global Var = 5; |/Local variable with the same name as the global one
cout<<"Local globalVar: "<<globalVar <<endl;
cout<<"Global globalVar: "<K:globalVar <<endl;
Outer outerObj:
Outer::Inner innerObj;
outerObj.displayStaticVar();
innerObj.displaylnnerVar();
return 0;
int main(0
int *arr:
int size;
cout<<"Enter array size:";
cin>>size;
arr = new int[size ]; //Dynamic memoryallocation
cout<<"nMemory allocated to array dynamically... "<<endl:
cout<<"nEnter array elements:";
for(inti=0;i<size;i++)
cin>>arr[i];
cout<<"\nArray elements are: ";
for(inti=0:;i<size;i++)
cout<<arrlil<<"";
b) Abstract classes
c) Inheritance types
e) Virtual functions
CODE -
int tempFnuml.val+x;
return temp;
int main()
int nl,n2;
cout<<"Enter a number nl:":
cin>>nl;
cout<<"Enter a number n2:":;
cin>>n2:
num nml(nl);
cout<<"nl="<<numl.get value()<<endl;
cout<<"-n1="<<-numl<<endl;
cout<<"nl+n2="<<num1+n2<<endl;
cout<<"'
return 0;n2+n]="<<n2+num1<<endl:
b) Abstract classes
public:
Rectangle(double 1, double w) : length(l), width(w)!
I/Implement the pure virtual functions
double area() const override
int main()
return 0:
c) Inheritance types
|/ Base class
class Animal
public:
void eat()
public:
void fly()
cout << "Bird is flying." << endl;
}:
};
class Tiger : public Animal
public:
void growl)
cout << "Tiger is growling." << endl:
};
/Derived class 5 (Hybrid Inheritance)
class Liger : public Lion, public Tiger
public:
void specialAbility()
{
cout << "Liger has a special ability." << endl;
int main0
{
/ Single Inheritance Example
Dog dog:
dog.eat();
dog.bark);
cout<<"nn":
Tiger tiger;
tiger.eat();
tiger.growl0;
cout<<"nn":
#include <iostream>
I/Base class
class Animal
string name;
public:
void eat()
cout << "Animal is eating." << endl;
class Tiger: virtualpublic Animal /lAnimal made virtual base class in hierarchy
public:
void growl)
cout <<"Tiger is growling." << endl;
int main0
Liger liger;
liger.eat();
liger.roar();
liger.growl);
liger.specialAbility();
return 0;
e) Virtualfunctions
public:
Circle(double r) : radius(r) {}
|/ Override the base class virtual function
double calculateArea) override
private:
double width;
double height;
public:
Rectangle(double w, double h) : width(w), height(h)
function
//Override the base class virtual
double calculateArea) override
};
int main()