0% found this document useful (0 votes)
38 views

CSE 111 Lab Report BUBT

The document provides examples of C++ code demonstrating various object-oriented programming concepts: 1) It shows examples of I/O statements, creating classes, a class implementing a stack, function overloading, inheritance, object pointers, structures, and inline functions. 2) A string class example allocates memory for a string and releases it in the destructor. 3) A constructor example initializes an integer member variable, and another shows passing parameters to a constructor.

Uploaded by

Mredul Adhikary
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

CSE 111 Lab Report BUBT

The document provides examples of C++ code demonstrating various object-oriented programming concepts: 1) It shows examples of I/O statements, creating classes, a class implementing a stack, function overloading, inheritance, object pointers, structures, and inline functions. 2) A string class example allocates memory for a string and releases it in the destructor. 3) A constructor example initializes an integer member variable, and another shows passing parameters to a constructor.

Uploaded by

Mredul Adhikary
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Chapter 1: An Overview of C++

Ex-1: efficient way to code the I/O statements:


Code:
# include <iostream>
using namespace std;
int main ()
{
cout << "Name:Mredul Adhikary"<< endl;
cout << "ID: 20234103429\n"<< endl;

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-2: create classes


Code:
# include <iostream>
using namespace std ;
class myclass{
int a ;
public :
void set_a ( int num ) ;
int get_a () ;
};
void myclass :: set_a ( int num ){
a = num ;
}
int myclass :: get_a (){
return a ;
}
int main (){
cout << "Name:Mredul Adhikary"<< endl;
cout << "ID: 20234103429\n"<< endl;

myclass ob1 , ob2 ;


ob1 . set_a (10) ;
ob2 . set_a (99) ;
cout << ob1 . get_a () << " \ n " ;
cout << ob2 . get_a () << " \ n " ;
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 ();
};

void stack:: init (){


tos = 0;
}
void stack :: push ( char ch ){
if ( tos == SIZE ){
cout << " stack is full ";
return;
}
stck [ tos ] = ch ;
tos ++;
}

char stack :: pop (){


if ( tos ==0)
{
cout << " Stack is empty ";
return 0;
}
tos--;
return stck [tos];
}
int main (){
cout << "Name: Mredul Adhikary "<< endl;
cout << "ID: 20234103429\n"<< endl;

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:

Ex-4: Function Overloading for absolute values


code:
# include < iostream >
using namespace std ;
int abs ( int n ){
cout << " In integer abs () \ n " ;
return n <0 ? -n : n ;
}
long abs ( long n ){
cout << " In long abs () \ n " ;
return n <0 ? -n : n ;
}
double abs ( double n ){
cout << " In double abs () \ n " ;
return n <0 ? -n : n ;
}
int main (){
cout << "Name: Mredul Adhikary"<< endl;
cout << "ID: 20234103429\n"<< endl;

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:

Ex-5: Function Overloading for date


Code:
#include <iostream>
using namespace std ;
void date( char *date ){
cout << " Date : " << date << "\n" ;
}
void date ( int month , int day , int year ){
cout << " Date : " << month << "/" ;
cout << day << " /" << year << "\n" ;
}
int main(){
cout << "Name: Mredul Adhikary"<< endl;
cout << "ID: 20234103429\n"<< endl;

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:

Chapter-2: Introducing Classes

Ex-1: class that contains a constructor and a destructor function:


Code:
#include <iostream>
using namespace std;
class myclass {
int a;
public :
myclass ();
void show ();
};
myclass :: myclass () {
cout << "In constructor \n";
a = 10;
}
void myclass :: show () {
cout << a;
}
int main () {
cout << "Name: Mredul Adhikary"<< endl;
cout << "ID: 20234103429\n"<< endl;

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:

Ex-3: Constructor with Parameters.


Code:
# include <iostream>
using namespace std;
class myclass {
int a;
public :
myclass (int x);
void show ();
};
myclass :: myclass (int x){
cout << "In constructor \n";
a = x;
}
void myclass :: show () {
cout << a << "\n";
}
int main () {
cout << "Name: Mredul Adhikary "<< endl;
cout << "ID: 20234103429\n"<< endl;

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 ();
};

void B:: set_i (int n) {


i = n;
}
int B:: get_i () {
return i;
}
void D:: set_j (int n) {
j = n;
}
int D:: mul() {
return j*get_i ();
}
int main () {
cout << "Name: Mredul Adhikary"<< endl;
cout << "ID: 20234103429\n"<< endl;

D ob;
ob. set_i (10);
ob. set_j (4);
cout << "Multiplication: "<<ob.mul()<< endl;
return 0;
}

Output:

Ex-5: Object Pointers


Code:

#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:

Ex-6: OBJECT POINTERS


Code:
#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 (420) ;
myclass * p;
p = & ob;
cout << " Value using object : " << ob.get() ;
cout << " \n " ;
cout << " Value using pointer : " << p->get () ;
return 0;
}
Output:

Ex-7: Create a program with structure


Code:
#include <iostream>
#include <cstring>
using namespace std;
struct st_type {
st_type ( double b, char * n );
void show ();
private:
double balance;
char name [40];
};
st_type :: st_type ( double b, char * n ) {
balance = b;
strcpy (name,n) ;
}
void st_type :: show () {
cout << " Name : " << name ;
if ( balance <0.0) {
cout << " ** " ;
cout << " \n " ;
return;
}
cout << " : $ " << balance << endl;

}
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-8: Inline Function


Code:
#include <iostream>
using namespace std ;
inline int even ( int x ) {
return !( x %2) ;
}
int main () {
cout << "Name:Mredul Adhikary "<< endl;
cout << "ID: 20234103429\n"<< endl;
if ( even (10) )
cout << " 10 is even \n " ;
if ( even (11) )
cout << " 11 is even \n " ;
return 0;}
Output:
Chapter-03:A Closer Look at Classes

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;

samp a (10), b (2) ;


cout << sqr_it ( a ) << " \n " ;
cout << sqr_it ( b ) << " \n " ;
return 0;
}

Output:

Ex-3: RETURNING OBJECTS FROM FUNCTIONS


Code:
# include <iostream>
# include <cstring>
using namespace std ;
class samp{
char s [80];
public :
void show (){ cout << s << " \n " ; }
void set ( char * str ) { strcpy (s, str ) ; }
};
samp input (){
char s [80];
samp str ;
cout << " Enter a string : " ;
cin >> s ;
str.set ( s ) ;
return str ;
}
int main (){
cout << "Name: Mredul Adhikary"<< endl;
cout << "ID: 20234103429\n"<< endl;

samp ob ;
ob = input () ;
ob.show () ;
return 0;
}
Output:

Ex-4: INTRODUCTION TO FRIEND FUNCTIONS


Code:
# include <iostream>
using namespace std ;
class myclass
{
int n, d ;
public :
myclass ( int i, int j )
{
n = i ;
d = j ;
}
friend int isfactor ( myclass ob ) ;
};
int isfactor ( myclass ob )
{
if (!( ob . n % ob . d ) )
return 1;
else
return 0;
}
int main ()
{
cout << "Name: Mredul Adhikary"<< endl;
cout << "ID: 20234103429\n"<< endl;

myclass ob1 (10, 2), ob2 (13, 3) ;


if ( isfactor ( ob1 ) )
cout << " 2 is a factor of 10\n " ;
else
cout << " 2 is not a factor of 10\n " ;
if ( isfactor ( ob2 ) )
cout << " 3 is a factor of 13\n " ;
else
cout << " 3 is not a factor of 13\n " ;
return 0;
}

Output:

You might also like