CSE 111 Lab Report BUBT
CSE 111 Lab Report BUBT
int i , j ;
double d ;
i = 10;
j = 20;
d = 99.101;
cout << " Here are some values : " ;
cout << i << ’ ’ << j << ’ ’ << d ;
return 0;
}
Output:
Ex-3: program creates a class called stack that implements a stack that can be used to
store characters:
Code:
# include <iostream>
using namespace std;
# define SIZE 10
class stack {
char stck [ SIZE ];
int tos ;
public:
void init () ;
void push (char ch ) ;
char pop ();
};
stack s1,s2;
int i ;
s1.init() ;
s2.init() ;
s1.push('a') ;
s2.push ( 'x') ;
s1.push ( 'b') ;
s2.push ( 'y') ;
s1.push ( 'c') ;
s2.push ( 'z') ;
for ( i =0;i <3; i ++)
cout<< " Pop s1 : " << s1 . pop () << "\n" ;
for ( i =0;i <3; i ++)
cout<< " Pop s2 : " << s2 . pop () << "\n" ;
return 0;
}
Output:
cout << " Absolute value of -10: " << abs ( -10) << " \ n \ n " ;
cout << " Absolute value of -10 L : " << abs ( -10 L ) << " \ n \
n ";
cout << " Absolute value of -10.01: " << abs ( -10.01) << "
\n\n";
return 0;
}
Output:
date("8/12/99");
date(8,12,99);
return 0;
}
Output:
Ex-6: Overloaded functions with differ in the number of arguments
Code:
# include < iostream >
using namespace std ;
void f1 ( int a )
{
cout << " In f1 ( int a ) \ n " ;
}
void f1 ( int a , int b )
{
cout << " In f1 ( int a , int b ) \ n " ;
}
int main ()
{
cout << "Name: Mredul Adhikary "<< endl;
cout << "ID: 20234103429\n"<< endl
f1 (10) ;
f1 (10 , 20) ;
return 0;
}
Output:
myclass ob;
ob. show ();
return 0;
}
Output:
Ex-2: s a simple string class, called strtype, that contains a string and its length. When
a strtype object is created, memory is allocated to hold the string and its initial length
is set to 0. When strtype object is destroyed, that memory is released.
Code:
# include <iostream>
# include <cstring>
# include <cstdlib>
using namespace std;
# define SIZE 25
class strtype {
char *p;
int len;
public:
strtype (); // constructor
~ strtype (); // destructor
void set ( char *ptr );
void show ();
};
strtype :: strtype () {
p = ( char *) malloc ( SIZE );
if (!p) {
cout << " Allocation error \n";
exit (1);
}
*p = '\0';
len = 0;
}
strtype ::~ strtype (){
cout << "Freeing p\n";
free (p);
}
void strtype :: set ( char *ptr ) {
if( strlen (p) >= SIZE ) {
cout << "String too big \n";
return;
}
strcpy (p, ptr);
len = strlen (p);
}
void strtype :: show () {
cout << p << "\n length: " << len ;
cout << "\n";
}
int main () {
cout << "Name: Mredul Adhikary "<< endl;
cout << "ID: 20234103429\n"<< endl;
strtype s1 , s2;
s1.set (" This is a test.");
s2.set ("I like C++. ");
s1. show ();
s2. show ();
return 0;
}
Output:
myclass ob (4) ;
ob. show ();
return 0;
}
Output:
Ex-4: Inheritance
Code:
#include <iostream>
using namespace std;
class B {
int i;
public :
void set_i (int n);
int get_i ();
};
class D : public B
{
int j;
public :
void set_j (int n);
int mul ();
};
D ob;
ob. set_i (10);
ob. set_j (4);
cout << "Multiplication: "<<ob.mul()<< endl;
return 0;
}
Output:
#include <iostream>
using namespace std;
class myclass {
int a;
public :
myclass (int x);
int get ();
};
myclass :: myclass(int x) { a = x; }
int myclass :: get () { return a; }
int main () {
cout << "Name: Mredul Adhikary"<< endl;
cout << "ID: 20234103429\n"<< endl;
myclass ob(120) ;
myclass *p;
p = &ob;
cout << " Value using object : " << ob.get();
cout << "\n";
cout << " Value using pointer : " << p->get();
return 0;
}
Output:
myclass ob (420) ;
myclass * p;
p = & ob;
cout << " Value using object : " << ob.get() ;
cout << " \n " ;
cout << " Value using pointer : " << p->get () ;
return 0;
}
Output:
}
int main () {
cout << "Name: Mredul Adhikary"<< endl;
cout << "ID: 20234103429\n"<< endl;
st_type acc1 (1000,"Mredul" );
st_type acc2 (500,"Mre" );
acc1.show ();
acc2.show ();
return 0;
}
Output:
Ex-1:Assigning Objects
Code:
# include <iostream>
using namespace std ;
class myclass{
int a, b ;
public :
void set ( int i, int j ) {
a = i ;
b = j ;
}
void show () {
cout << a <<" "<< b << " \n " ;
}
};
int main () {
cout << "Name: Mredul Adhikary "<< endl;
cout << "ID: 20234103429\n"<< endl;
myclass o1, o2 ;
o1 . set (10, 4) ;
o2 = o1 ;
o1 . show () ;
o2 . show () ;
return 0;
}
Output:
Ex-2: Passing Objects to Pointers
Code:
#include<iostream>
using namespace std ;
class samp{
int i ;
public :
samp ( int n ) {
i = n ;
}
int get_i () {
return i ;
}
};
int sqr_it ( samp o ){
return o . get_i () * o . get_i () ;
}
int main (){
cout << "Name: Mredul Adhikary"<< endl;
cout << "ID: 20234103429\n"<< endl;
Output:
samp ob ;
ob = input () ;
ob.show () ;
return 0;
}
Output:
Output: