Oops

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

LAB FILE

OBJECT ORIENTED PROGRAMMING USING


C++

BACHELOR OF TECHNOLOGY

COMPUTER SCIENCE AND ENGINEERING

Submitted to: Submitted by:

Mr. Dev Kumar Chaudhary Siddarth Katakamsetti

CSE Dept. A2305218035, 3CSE 1Y

AMITY SCHOOL OF ENGINEERING &


TECHNOLOGY AMITY UNIVERSITY NOIDA
Experiment 1
Objective: Simple C++ Programs to implement various Control Structures
a. if else condition
b. switch case
c. for loop
d. while loop
Program:
#include<iostream>
#include<conio.h>
using namespace std;
void main()
{ int a, b;
cout<<"Enter A and B :";
cin>>a>>b;
cout<<"If else (to find larger no.) ";
if(a>b)
cout<<"\nA is larger";
else
cout<<"\nB is larger";
cout<<"\nSwitch (to find smaller no.) ";
switch(a<b)
{ case 0 : cout<<"\nB is smaller";
case 1 : cout<<"\nA is smaller";
}
cout<<"\n Do while loop(to decrement B till 0) ";
do
{ b--;
cout<<" "<<b;
}while(b!=0);
cout<<"\nFor Loop (to print all no. less than A) ";
for(int i=0;i<a;i++)
cout<<" "<<i;
cout<<"\n While loop(to increment B till 5) ";
while(b<=5)
{ cout<<b;
b++;
}
getch();
}
Output:

Experiment 2
Objective: Programs to understand structures and unions.
a. Structure
b. Union
Program:
#include <iostream>
#include <string.h>
#include <conio.h>
using namespace std;
struct structureA
{
int integer;
float decimal;
char name[20];
};
union unionB
{
int integer;
float decimal;
char name[20];
};

void main()
{clrscr();
struct structureA s;
union unionB u;
cout<<"sizeof structure : "<<sizeof(s);
cout<<"\nsizeof union : "<< sizeof(u);
cout<<"\n Accessing all members at a time:";
s.integer = 153;
s.decimal = 90;
strcpy(s.name, "helloworld");
cout<<"\nstructure data: integer:" <<s.integer<<"\ndecimal:"<<
s.decimal<<"\nname:"<< s.name;
u.integer = 153;
u.decimal = 90;
strcpy(u.name, "helloworld");
cout<<"\nunion data: integer:" <<u.integer<<"\ndecimal:"<<
u.decimal<<"\nname:"<< u.name;
cout<<"\n Accessing one member at time:";
cout<<"\nstructure data:";
s.integer = 240;
cout<<"\ninteger: "<< s.integer;

s.decimal = 120;
cout<<"\ndecimal: "<< s.decimal;

strcpy(s.name, "C programming");


cout<<"\nname: "<<s.name;

cout<<"\n union data:";


u.integer = 240;
cout<<"\ninteger: "<<u.integer;

u.decimal = 120;
cout<<"\ndecimal: "<<u.decimal;

strcpy(u.name, "C programming");


cout<<"\nname: "<<u.name;

cout<<"\nAltering a member value:\n";


s.integer = 1215;
cout<<"structure data: integer:" <<s.integer<<"\ndecimal:"<<
s.decimal<<"\nname:"<< s.name;

u.integer = 1215;
cout<<"\nunion data: integer:" <<u.integer<<"\ndecimal:"<<
u.decimal<<"\nname:"<< u.name;
getch();
}
Output:
Experiment 3
Objective: Programs to understand Pointer Arithmetic.
Program:
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{ int i, a[5];
int *p;

p=a;
for(i=0;i<5;i++)
cin>>a[i];
for(i=0;i<5;i++)
{ cout<<*p;
p++;
}
p--;
cout<<"\n";
for(i=0;i<5;i++)
{ cout<<*p;
p--;
}
getch();
}
Output:
Experiment 4
Objective: Programs to understand Functions and Recursion.
a. Function
b. Recursion
Program:
#include<iostream>
#include<conio.h>
using namespace std;
void square(int a)
{ int c;
c=a*a;
cout<<"Square of a :"<<c;
}
int fact(int n)
{ if(n<=1)
return 1;
else
return n*fact(n-1);
}
int main()
{ int a;

cin>>a;
square(a);
cout<<"\nFactorial of a is : "<<fact(a);
getch();
}
Output:

Experiment 5
Objective: Programs to understand Inline Functions
Program:
#include<iostream>
using namespace std;
#include<conio.h>
class A
{
int a,b,summ,dif,prod;
float div;
public:
void get();
void sum();
void difference();
void product();
void division();
};
inline void A :: get()
{
cout << "Enter first value:";
cin >> a;
cout << "Enter second value:";
cin >> b;
}

inline void A :: sum()


{
summ = a+b;
cout << "Addition of two numbers: " <<summ << "\n";
}

inline void A :: difference()


{
dif = a-b;
cout << "Difference of two numbers: " <<dif << "\n";
}

inline void A :: product()


{
prod = a*b;
cout << "Product of two numbers: " << prod << "\n";
}

inline void A ::division()


{
div=a/b;
cout<<"Division of two numbers: "<<div<<"\n" ;
}

int main()
{
cout << "Program using inline function\n";
A ob1;
ob1.get();
ob1.sum();
ob1.difference();
ob1.product();
ob1.division();
getch();
}
Output:
Experiment 6
Objective: Programs to understand Different call mechanisms
a. Call by reference
b. Call by value
Program:
#include<iostream>
using namespace std;
#include<conio.h>
void callbyvalue(int c, int d)
{ c=c+d;
d=c-d;
c=c-d;
}
void callbyreference(int &c, int&d)
{ c=c+d;
d=c-d;
c=c-d;
}
int main()
{ int a ,b;

cout<<"Enter Values of a and b : ";


cin>>a>>b;
cout<<"Calling callbyvalue function to interchange a and b\n";
callbyvalue(a,b);
cout<<"a="<<a<<" b="<<b<<" no change made to orignal variables";
cout<<"\nCalling callbyreference function ";
callbyreference(a,b);
cout<<"a="<<a<<" b="<<b<<"\nChange made directly on orignal variables";
getch();
}
Output:

Experiment 7
Objective: Programs to understand Storage specifiers
Program:
#include<iostream>
using namespace std;
#include<conio.h>
int b;
void autostorageclass()
{ auto int a;
cout<<"Auto storage class variable 'a' declared";
cout<<"\nInitial value of a(garbage value): "<<a;
cout<<"\nEnd Lifetime of a\n";
}
void externstorageclass()
{ extern int b;
cout<<"\nExtern storage class variable 'b'is declared outside function block";
cout<<"\nInitial value of b: "<<b;
cout<<"\n Lifetime of b is throughout the program\n";
}
void staticstorageclass()
{ static int c;
if(c!=5)
{
cout<<"\nStatic storage class varaible 'c' declared";
cout<<"\nInitial value of c: "<<c;
c=5;
cout<<"\nChange value of C to 5";
cout<<"\ncalling the same function again";
staticstorageclass();
}
else
{
cout<<"\nValue remains 'c'same i.e"<<c;
cout<<"\nlifetime is through out the program but vairable is local to
function\n";
}
}
void registerstorageclass()
{ register int d;
cout<<"\nregister storage class variable 'd' declared";
cout<<"\nInitial value of d(garbage value) :"<<d;
cout<<"\nEnd of lifetime of 'd'";
}
int main()
{
autostorageclass();
externstorageclass();
staticstorageclass();
registerstorageclass();
getch();
}
Output:

Experiment 8
Objective: Programs to understand Constructors and Destructors
Program:
#include<iostream>
using namespace std;
#include<conio.h>
class A
{ int x;
int y;
public :
A()
{ cout<<"\nDefault constructor";
x=0;
y=0;
}
A(int c, int d)
{ cout<<"\nParamaterised constructor";
x=c;
y=d;
}
A(A &ob)
{ cout<<"\n Copy constructor";
x=ob.x;
y=ob.y;
}
~A()
{ cout<<"Destructor executed";
}
void getdata()
{ cout<<"\nx="<<x<<"\ny="<<y;
}
};
int main()
{
cout<<"\n Declaring Ob1 ";
A ob1;
ob1.getdata();
cout<<"\n Declaring Ob2 with parameters( 5 and 6)";
A ob2(5,6);
ob2.getdata();
cout<<"\n Declaring and Initializing ob3 with ob2";
A ob3 = ob2;
ob3.getdata();
getch();
}
Output:
Experiment 9
Objective: Programs to understand use of ‘this’ pointer using class.
Program:

#include<iostream>
using namespace std;

class Test
{
private:
int x;
int y;
public:
Test(int x = 0, int y = 0) { this->x = x; this->y = y; }
Test &setX(int a) { x = a; return *this; }
Test &setY(int b) { y = b; return *this; }
void print() { cout << "x = " << x << " y = " << y << endl; }
};

int main()
{
Test obj1(5, 5);

// Chained function calls. All calls modify the same object


// as the same object is returned by reference
obj1.setX(10).setY(20);
obj1.print();
return 0;
}
Output:

Experiment 10
Objective: Programs to Implement Inheritance and Function Overriding.
a. Multiple inheritance –Access Specifiers

b. Hierarchical inheritance – Function Overriding /Virtual Function

Program 10 (A):
#include<iostream>
using namespace std;
#include<conio.h>
class A
{ protected:
int a;
int cube;
void cubec()
{ cube = a*a*a;
}
public :
void getdata()
{ cout<<"Enter value of a :";
cin>>a;
cubec();
}
void putdata()
{ cout<<"\na ="<<a;
}
};
class B
{ int n;
public :
void getinfo()
{ cout<<"\nEnter value of n :";
cin>>n;
}
void display()
{ cout<<"\nn="<<n;
}
};
class C : public A, public B
{ int d;
public :
void input()
{ cout<<"\nEnter value of d :";
cin>>d;
}
void show()
{ cout<<"\nd="<<d;
cout<<"\na="<<a;
cout<<"\ncube="<<cube;
}
};
int main()
{
C ob1;
ob1.getdata();
ob1.getinfo();
ob1.input();
ob1.display();
ob1.show();
getch();
}
Output:

Program 10(B):
#include<iostream>
using namespace std;
#include<conio.h>
#include<stdio.h>
class company
{ char cname[20];
public :
void getdata()
{ cout<<"Enter company name :";
gets(cname);
}
void display()
{ cout<<"\nCompany name : ";
puts(cname);
}
};
class brand : public company
{ char bname[20];
public :
void input()
{ cout<<"Enter Brand name :";
gets(bname);
}
void display()
{ cout<<"Brand name : ";
puts(bname);
}
};
class product : public brand
{ char pname[20];
public :
void getinfo()
{ cout<<"Enter product name :";
gets(pname);
}
void display()
{ cout<<"Product name : ";
puts(pname);
}
};
int main()
{
product ob1;
ob1.getdata();
ob1.input();
ob1.getinfo();
ob1.company::display();
ob1.brand::display();
ob1.display();
getch();
}
Output:
Experiment 11
Objective: Programs to Implement Inheritance and Function Overriding.
a. Multiple inheritance –Access Specifiers

c. Hierarchical inheritance – Function Overriding /Virtual Function


Program 11A:
#include<iostream>
using namespace std;
#include<conio.h>
class WOW
{ int a;
int b;
public :
void getdata()
{ cout<<"Enter values of A and B :";
cin>>a;
cin>>b;
}
void putdata()
{ cout<<"\nA="<<b<<"\nB="<<b;
}
void operator -(void);
};
void WOW::operator -()
{ a=-a;
b=-b;
}
int main()
{
WOW ob1;
ob1.getdata();
ob1.putdata();
-ob1;
cout<<"\n-ob1 :";
ob1.putdata();
getch();
}
Output:

Program 11B:
#include<iostream>
using namespace std;
#include<conio.h>
class wow
{ int a;
int b;
public :
void getdata()
{ cout<<"Enter values of a and b :";
cin>>a;
cin>>b;
}
void putdata()
{ cout<<"\na="<<a<<" b="<<b;
}
friend wow operator +(int ,wow&);
};
wow operator +(int e ,wow &ob)
{ wow c;
c.a= e+ob.a;
c.b= e+ob.b;
return(c);
}
int main()
{
wow ob1,ob2;
ob1.getdata();
cout<<"\nob1 values : ";
ob1.putdata();
ob2 = 2+ob1;
cout<<"\nOb2 = 2+ob1\n"<<"Ob2 values : ";
ob2.putdata();
getch();
}
Output:

Experiment 12
Objective: Programs to Understand Friend Function & Friend Class.
a. Friend Function
b. Friend class
Program 12A:
#include<iostream>
using namespace std;
#include<conio.h>
class A
{ int a;
int b;
public :
void getdata()
{ cout<<"Enter a and b :";
cin>>a;
cin>>b;
}
void putdata()
{cout<<"a="<<a;
cout<<" b="<<b;
}
friend int sum(A&);
};
int sum(A&ob)
{ int sum;
sum=ob.a+ob.b;
return sum;
}
int main()
{ A ob1;

ob1.getdata();
ob1.putdata();
cout<<"\nSum of a and b of ob1 is :"<< sum(ob1);
getch();
}
Output:

Program 12B:
#include <iostream>
using namespace std;
#include <conio.h>
class A {
private:
int a;

public:
A() { a = 0; }
friend class B; // Friend Class
};

class B
{
private:
int b;

public:
void showA(A& x)
{ cout << "A::a=" << x.a;
} // Since B is friend of A, it can access
// private members of A
};

int main()
{
A a;
B b;
b.showA(a);
getch();}
Output:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy