COURSE CODE-CAP1003L
INTRODUCTION TO OBJECT
ORIENTED CONCEPTS USING C++
SCHOOL OF ENGINEERING AND SCIENCES (SOES)
SUBMITTED BY
Student Name Ashutosh Kumar
Enrollment Number 230160255018
Section/Group BCA (Machine Learning)
Semester III Semester – 2nd YEAR
Faculty Name Mrs. Shipra Katria
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. Write a program to understand the 04
concept of class/object, member
function definition inside and outside
the class.
3. Write a program to check the working of 05
NEW/DELETE operators.
4. Write a program for stack 06-07
implementation using
constructor.
5. Write a program to show 08-09
the constructor overloading.
6. Write a program to show the 10-11
single inheritance.
7. Write a program to show the 12-13
ambiguity problem in multiple
inheritance.
Write a program to show 14-15
8.
diamond problem in hybrid
inheritance.
Write a program to show the function 16
9. overloading.
Write a program to show unary operator 17-18
10. overloading.
Write a program to show binary operator 19
11. overloading.
Write a program to show 20
12. Function template
Write a program to show 21
13. Class template
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;
cout << "Enter an integer:
"; cin >> 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
AIM: Write a program to show the single inheritance.
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
AIM: Write a program to show the ambiguity problem in multiple
Inheritance.
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
AIM: Write a program to show diamond problem in hybrid
Inheritance.
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
AIM: Write a program to show the function overloading
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
AIM: Write a program to show unary operator overloading.
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;
template <typename T>
T add(T num1, T num2)
{ return (num1 +
num2);
}
int main()
{ int
result1;
double result2;
// calling with int
parameters result1 =
add<int>(2, 3);
cout << "2 + 3 = " << result1 << endl;
// calling with double
parameters result2 =
add<double>(2.2, 3.3);
cout << "2.2 + 3.3 = " << result2 << endl;
return 0;
}
Output:
20
EXPERIMENT-13
AIM: Write a program to show Function template.
- 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() {
// create object with int type
Number<int> numberInt(7);
// create object with double type
Number<double> numberDouble(7.7);
cout << "int Number = " << numberInt.getNum() << endl;
cout << "double Number = " << numberDouble.getNum() << endl;
return 0;
}
Output:
21
22