Yash Oops
Yash Oops
INTRODUCTION TO OBJECT
ORIENTED CONCEPTS USING C++
SUBMITTED BY
Student Name Ashutosh Kumar
Enrollment Number 230160255018
1
INDEX
S.N PARTICULARS PG DATE SIGNATURE
O NO
1. Write a program to check whether 03
the given number is even or odd.
2
Experiment 1
AIM: Write a program to check whether the given number is even
or Odd.
SOURCE CODE
#include
<iostream> using
namespace std;
int main()
{ int n;
if ( n % 2 == 0)
cout << n << " is
even."; else
cout << n << " is odd.";
return 0;
}
OUTPUT:
3
Experiment 2
AIM: Write a program to understand the concept of class/object,
member function definition inside and outside the class.
SOURCE CODE
#include
<iostream> using
namespace std;
class a{
public:
int a;
string name;
};
int main (){
a AA;
AA.a = 19;
AA.name = "Riyan";
cout<<AA.a<<endl;
cout<<AA.name<<endl;
}
OUTPUT:
4
Experiment 3
AIM: Write a program to check the working of NEW/DELETE
Operators.
SOURCE CODE
#include<iostream>
using namespace
std;
int main(){
int * a = new int;
*a = 10;
cout<<"Number of this pointer is : "<<*a;
delete a;
return 0;
}
5
Experiment 4
AIM: Write a program for stack implementation using constructor.
SOURCE CODE
#include<iostream>
using namespace
std; class STACK
{ public:
int top;
int
arr[MAX];
STACK() {
top = -1;
bool isempty()
{ return = -1;
}
}
void push(int value) {
if(top == MAX – 1) {
cout<<“Stack overflow! Cannot push”<<value<<endl;
}
else {
arr[++top] = value;
cout<<value<<“pushed into
stack”<<endl;
}
}
int pop()
{ if(Isempty())
{ return -1;
cout<<“Stack underflow! Cannot pop from an empty stack ”<<endl;
}
else
{ return[top-
-];
}
}
int peek() {
6
if(isEmpty()) {
cout<<“Stack is empty!”<<endl;
return -1;
}
else {
return arr[top];
}
}
};
int main()
{ STACK
STACK;
STACK.push(10)
;
STACK.push(20)
;
STACK.push(30)
;
cout<<“top element is ”<<STACK.peek()<<endl;
cout << STACK.pop() << " popped from stack." << endl;
cout << "Top element is: " << STACK.peek() << endl;
STACK.push(40);
cout << "Top element is: " << STACK.peek() <<endl;
return 0;
}
OUTPUT:
7
Experiment 5
AIM: Write a program for constructor overloading.
SOURCE CODE
#include<iostream>
using namespace
std;
class saul{
int x,y;
public:
saul() {
x = 10;
y = 20;
}
saul(int a, int b){
x = a;
y = b;
}
saul(saul&d)
{ x = d.x;
y = d.y;
}
void display_details(){
cout<<x<<endl;
cout<<y<<endl;
}
};
int main (){
saul AA;
saul BB(15,25);
saul CC = BB;
AA.display_details();
BB.display_details();
CC.display_details();
}
8
OUTPUT:
9
Experiment 6
SOURCE CODE
#include<iostream>
using namespace
std;
class saul{
protected:
int x;
public:
void input(){
cout<<"enter
number"<<endl; cin>>x;
}
};
class jesse:public saul{
int y;
public:
void takedetails(){
cout<<"enter number 2"<<endl;
cin>>y;
}
void putdetails()
{ cout<<"addition = "<<x+y;
}
};
int main()
{ jesse aa;
aa.input()
;
aa.takedetails();
aa.putdetails();
}
10
Output:
11
EXPERIMENT-7
SOURCE CODE
#include<iostream>
using namespace
std;
class A{
public:
void showdetails(){
cout<<"Silent hill 2 remake"<<endl;
}
};
class B {
public:
void showdetails(){
cout<<"resident evil 4 remake"<<endl;
}
};
class C :public A,public B
{ public:
void showdetails()
{ cout<<"alan wake
2"<<endl;
}
};
int main(){
C aa;
aa.showdetails();
return 0;
}
12
To solve the ambiguity problem, we use scope resolution.
SOURCE CODE
#include<iostream>
using namespace
std;
class A{
public:
void showdetails(){
cout<<"Silent hill 2 remake"<<endl;
}
};
class B {
public:
void showdetails(){
cout<<"resident evil 4 remake"<<endl;
}
};
class C :public A,public B
{ public:
void showdetails()
{ A::showdetails();
B::showdetails();
cout<<"alan wake 2"<<endl;
}
};
int main(){
C aa;
aa.showdetails();
return 0;
}
Output:
13
EXPERIMENT-8
SOURCE COD
#include<iostream
> using namespace
std;
#include<iostream
> using namespace
std; class A {
public:
void show()
{ cout<<“Batman”<<en
dl;
}
};
class B: virtual public A
{ void showdetails(){
cout<<"spiderman"<<endl;
}
};
class C: virtual public A
{ void printdetails(){
cout<<"superman"<<endl;
}
};
class D: virtual public B, virtual public C {
};
int main() {
D obj1;
obj1.show(
); return 0;
}
14
OUTPUT:
15
EXPERIMENT-9
SOURCE CODE
#include<iostream
> using namespace
std; class A {
public:
void func(int a, int b) {
cout<<“The addition is ”<<a+b<<endl;
}
void func(float x, int y) {
cout<<“The multiplication is ”<<x*y<<endl;
}
};
int main ()
{ A obj;
Obj.func(1,2)
;
}
Output:
16
EXPERIMENT-10
SOURCE CODE(prefix)
#include<iostream>
using namespace
std; class demo{
int x;
public:
void getdetails()
{ cout<<"enter
num"<<endl; cin>>x;
}
void putdetails()
{ cout<<x<<en
dl;
}
void operator ++()
{ x = x + 1;
}
};
int main()
{ demo aa;
aa.getdetails()
;
cout<<"orginal value"<<endl;
++aa;
aa.putdetails()
;
cout<<"value
added"<<endl; return 0;
}
Output:
17
SOURCE CODE (postfix)
#include<iostream
> using namespace
std; class demo{
int
x;
public:
void getdetails()
{ cout<<"enter
num"<<endl; cin>>x;
}
void putdetails()
{ cout<<x<<en
dl;
}
void operator ++
(int){ x = x + 1;
}
};
int main()
{ demo aa;
aa.getdetails(
);
cout<<"orginal
value"<<endl; aa++;
aa.putdetails();
cout<<"value
added"<<endl; return 0;
}
Output:
18
EXPERIMENT-11
AIM: Write a program to show binary operator overloading.
SOURCE CODE
#include<iostream>
using namespace
std; class demo{
int a;
public:
void getdata(){
cout<<"enter num"<<endl;
cin>>a;
}
void putdata(){
cout<<"addtion is "<<a<<endl;
}
demo operator + (demo bb){
demo cc;
cc.a = a + bb.a;
return cc;
}
};
int main(){
demo
aa,bb,cc;
aa.getdata();
bb.getdata();
cc = aa +
bb;
cc.putdata();
return 0;
}
The Output:
19
EXPERIMENT-12
AIM: Write a program to show Function template.
SOURCE CODE
#include
<iostream> using
namespace std;
int main()
{ int
result1;
double result2;
// calling with int
parameters result1 =
add<int>(2, 3);
cout << "2 + 3 = " << result1 << endl;
return 0;
}
Output:
20
EXPERIMENT-13
- SOURCE CODE
#include
<iostream> using
namespace std;
// Class template
template <class
T> class Number
{
private:
// Variable of type
T T num;
public:
Number(T n) : num(n) {} // constructor
T getNum() {
return
num;
}
};
int main() {
return 0;
}
Output:
21
22