Oops
Oops
Oops
BACHELOR OF TECHNOLOGY
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;
u.decimal = 120;
cout<<"\ndecimal: "<<u.decimal;
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;
}
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;
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);
Experiment 10
Objective: Programs to Implement Inheritance and Function Overriding.
a. Multiple inheritance –Access Specifiers
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
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: