0% found this document useful (0 votes)
75 views52 pages

Lab 9-14

The document contains 6 programs related to C++ classes and OOP concepts. Program 1 defines a complex number class with methods to edit, display, add, subtract and multiply complex numbers. Program 2 adds a plus method to the complex number class that can add two complex numbers. Program 3 defines a time class. Program 4 sorts an integer array using nested for loops. Program 5 defines a complex number class and operates on an array of complex numbers. Program 6 defines a Matrix class with methods to input, display, add and subtract matrices.

Uploaded by

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

Lab 9-14

The document contains 6 programs related to C++ classes and OOP concepts. Program 1 defines a complex number class with methods to edit, display, add, subtract and multiply complex numbers. Program 2 adds a plus method to the complex number class that can add two complex numbers. Program 3 defines a time class. Program 4 sorts an integer array using nested for loops. Program 5 defines a complex number class and operates on an array of complex numbers. Program 6 defines a Matrix class with methods to input, display, add and subtract matrices.

Uploaded by

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

OOP C++ lab#9

Program#1
#include <iostream.h>

class complex

{ float real,imag; public:

complex () {real=0.0f; imag= 0.0f;}

complex (float a, float b) {real=a; imag=b;}

void edit()

{cout<<"\n\tEnter real part: ";cin >>real;

cout<<"\n\tEnter imaginary part: "; cin>>imag;}

void display()

{(imag>=0)? cout<<"\n\t"<<real<<"+j"<<imag<<endl:cout<<real<<"-"<<-imag<<endl;}

void plus(complex a)

{ complex sum(real+a.real,imag+a.imag);

cout<<"\n\tSum-->\t";sum.display();}

void minus(complex b)

{complex sub(real-b.real,imag-b.imag);

cout<<"\n\tSubtraction-->\t";sub.display();}

void multiply(complex c)

{complex multiply(real*c.real-imag*c.imag,real*c.imag+imag*c.real);

cout<<"\n\tMultiply-->\t";multiply.display();}};

void main ()

{ complex c1, c2(3,5),c3(8,9);

c1.edit(); c1.display();

Sohail Akhtar 12-EE-133 A2


OOP C++ lab#9

c2.plus(c3);

c3.minus(c1);

c1.multiply(c2);

#include <iostream.h>

class complex

{ float real,imag; public:

complex () {real=0.0f; imag= 0.0f;}

complex (float a, float b) {real=a; imag=b;}

void edit()

{cout<<"\n\tEnter real part: ";cin >>real;

cout<<"\n\tEnter imaginary part: "; cin>>imag;}

void display()

{(imag>=0)? cout<<"\n\t"<<real<<"+j"<<imag<<endl:cout<<real<<"-"<<-imag<<endl;}

complex plus(complex a,complex b)

{ return complex (a.real+b.real,a.imag+b.imag);}};

void main ()

{ complex c1, c2(3,5),c3(8,9);

c1.plus(c2,c3).display();

Output(12-EE-133)

Sohail Akhtar 12-EE-133 A2


OOP C++ lab#9

PROGRAM# 2
#include <iostream.h>

class complex

{ float real,imag; public:

complex () {real=0.0f; imag= 0.0f;}

complex (float a, float b) {real=a; imag=b;}

void edit()

{cout<<"\n\tEnter real part: ";cin >>real;

cout<<"\n\tEnter imaginary part: "; cin>>imag;}

void display()

{(imag>=0)? cout<<"\n\t"<<real<<"+j"<<imag<<endl:cout<<real<<"-"<<-imag<<endl;}

complex plus(complex a,complex b)

{ return complex (a.real+b.real,a.imag+b.imag);}};

void main ()

{ complex c1, c2(3,5),c3(8,9);

c1.plus(c2,c3).display();

Sohail Akhtar 12-EE-133 A2


OOP C++ lab#9

Output(12-EE-133)

PROGRAM# 3
#include <iostream.h>

#include <conio.h>

class time

{int hr,min; public:

time();

time (int,int);

void showtime()

{cout<<endl<<hr<<":"<<min;}};

time::time(){hr=0;min=0;}

time::time(int x,int y){hr=x;min=y;}

void main (void)

{time t;

t.showtime();

time t1(11,10);

t1.showtime();

Output(12-EE-133)

PROGRAM# 4
Sohail Akhtar 12-EE-133 A2
OOP C++ lab#9

#include<iostream.h>

#include<conio.h>

void main()

{int u,t,i,mna[4]={23,0,51,23};

for (u=3;u>=1;u--)

for (i=0;i<u;i++)

if (mna[i]<mna[i+1])

{ t=mna[i];

mna[i]=mna[i+1];

mna[i+1]=t;}

cout<<"sorted values"<<endl;

for (i=0;i<=3;i++)cout<<mna[i]<<endl;getch();}

Output(12-EE-133)

PROGRAM# 5
#include <iostream.h>

class complex

{ float real,imag; public:

complex () {real=0.0f; imag= 0.0f;}

complex (float a, float b) {real=a; imag=b;}

Sohail Akhtar 12-EE-133 A2


OOP C++ lab#9

void edit()

{cout<<"\n\tEnter real part: ";cin >>real;

cout<<"\n\tEnter imaginary part: "; cin>>imag;}

void display()

{(imag>=0)? cout<<"\n\t"<<real<<"+j"<<imag<<endl:cout<<real<<"-"<<-imag<<endl;}

void plus(complex a)

{ complex sum(real+a.real,imag+a.imag);

cout<<"\n\tSum-->\t";sum.display();}

void minus(complex b)

{complex sub(real-b.real,imag-b.imag);

cout<<"\n\tSubtraction-->\t";sub.display();}

void multiply(complex c)

{complex multiply(real*c.real-imag*c.imag,real*c.imag+imag*c.real);

cout<<"\n\tMultiply-->\t";multiply.display();}};

void main ()

{ complex a[5]={complex(),complex(4,-5),complex(10,20)};

a[0].edit();a[0].display();

a[1].plus(a[2]);

a[2].minus(a[0]);

a[0].multiply(a[1]);

Output(12-EE-133)

Sohail Akhtar 12-EE-133 A2


OOP C++ lab#9

PROGRAM# 6

#include<iostream.h>

#include<conio.h>

class Matrix

private:

int matrix[2][2];

public:

Matrix();

Matrix(int);

void input();

void display();

void add(Matrix a);

void subtract(Matrix a);

};

int main ( )

Sohail Akhtar 12-EE-133 A2


OOP C++ lab#9

clrscr();

Matrix M,M1(1),M2(2);

M1.display();

M1.input();

M.display();

M1.display();

M2.display();

M1.add(M2);

M1.subtract(M2);

getch();

return 0;

Matrix::Matrix()

for (int loop2=0;loop2<2;loop2++)

for (int loop1=0;loop1<2;loop1++)

matrix[loop2][loop1]=0;

Matrix::Matrix(int)

input();

void Matrix::input()

Sohail Akhtar 12-EE-133 A2


OOP C++ lab#9

cout<<"\n\t Enter values \n";

for (int loop5=0;loop5<2;loop5++)

for (int loop6=0;loop6<2;loop6++)

cout<<"enter next \t";

cin>>matrix[loop5][loop6];

void Matrix::display()

cout<<"\n\n\t Contents \n";

for (int loop7=0;loop7<2;loop7++)

for (int loop8=0;loop8<2;loop8++)

cout<<matrix[loop7][loop8]<<"\t";

cout<<"\n";

void Matrix::add(Matrix a)

for (int loop9=0;loop9<2;loop9++)

for (int loop10=0;loop10<2;loop10++)

a.matrix[loop9][loop10]=matrix[loop9][loop10]+a.matrix[loop9][loop10];

a.display();

void Matrix::subtract(Matrix a)

Sohail Akhtar 12-EE-133 A2


OOP C++ lab#9

for (int loop11=0;loop11<2;loop11++)

for (int loop12=0;loop12<2;loop12++)

a.matrix[loop11][loop12]=matrix[loop11][loop12]-a.matrix[loop11]
[loop12];

a.display();

Output(12-EE-133)

Sohail Akhtar 12-EE-133 A2


OOP C++ lab#10

PROGRAM# 1
#include <iostream.h>

void main ()

{ char A[]= {'W','e','l','l','C','o','m','e','\0'};

char B[15]="EED UET TAXILA";

char C[9]={71,111,0x6F,100,'B',0x79,A[7],041,0};

cout<<A<<"\t"<<sizeof(A)<<"\n"<<B<<"\t"<<sizeof(B)<<"\n"<<C<<"\t"<<sizeof(C)<<endl;

cout<<++C[0]<<A[1]<<B[12]<<char(108)<<C[2];}

Output(12-EE-133)

PROGRAM# 2
#include<iostream.h>

#include<conio.h>

#include<dos.h>

void main(void)

char x[]="uet taxila";

int i,j;

gotoxy(30,1);

cout<<x;

Sohail Akhtar 12-EE-133 A2


OOP C++ lab#10

for(i=0;x[i]!='\0';i++)

{for(j=1;j<=24;j++) {gotoxy(i+30,j); delay(40);gotoxy(i+30,j);cout<<" ";}

gotoxy(i+30,j);cout<<x[i];}

getch();

Output(12-EE-133)

PROGRAM# 3
#include<iostream.h>

#include<conio.h>

int main ( )

char a[100];

cout<<"\t\t enter for cin: ";

cin>>a;

Sohail Akhtar 12-EE-133 A2


OOP C++ lab#10

cout<<"output for cin="<<a<<endl;

cout<<"\n\t\t enter for cin.get() \n";

cin.get(a,25,'0'); //if space is used then enter terminates the program

cout<<"output for cin.get()="<<a<<endl;

return 0;}

Output(12-EE-133)

PROGRAM# 4
#include<iostream.h>
#include<conio.h>
#include<string.h>
const int SIZE=100;
class string
{
char str[SIZE];
int length;
public:
string()
{
strcpy(str,"empty");
length=5;
}
string(char array[])
{

Sohail Akhtar 12-EE-133 A2


OOP C++ lab#10

length=strlen(array);
if (length>SIZE)
cout<<"\n\n\t\t ERROR! \n\n";
else
strncpy(str,array,length);
str[length+1]='\0';
}
void input()
{
cout<<"\n enter values and use ctrl+z to terminate \n";
cin.getline(str,100,cin.eof());
length=strlen(str);
}
void output()
{
cout<<"output=\n"<<str<<endl;
cout<<"length="<<length<<endl;
}
void concatenate(char array1[])
{
if (strlen(str)+strlen(array1)>SIZE)
cout<<"\n\n\t\t ERROR! \n\n";
else
strcat(str,array1);
}
void compare(char array2[])
{
cout<<endl<<"result of strncmp=\n"<<strncmp(str,array2,5)<<endl;
cout<<endl<<"result of strcmp=\n"<<strcmp(str,array2)<<endl;
}
Sohail Akhtar 12-EE-133 A2
OOP C++ lab#10

};
int main ( )
{
clrscr();
char arr[SIZE]="j"; //by changing the size 'SIZE' of the array
//we can generate the error
for (int loop=0;loop<SIZE;loop++)
arr[loop+1]='\0';
string s,s1(arr);
s.output();
s.input();
s.output();
s.compare(arr);
s.concatenate(arr);
s.output();
getch();
return 0;}

Output(12-EE-133)

Sohail Akhtar 12-EE-133 A2


OOP C++ lab#10

PROGRAM# 5
#include <iostream.h>
#include <conio.h>
#include <string.h>
void main (void)
{
int b,c,d,e,f;
char a[5];
cout<<"\n\tEnter string: ";
cin>>a;
c=strlen(a);
d=c;
b=0;
f=0;
do
{
if(a[b]==a[c-1])
e=0;
else
e=1;
b++;
c--;
}while(b<=d,c>=1);
if(e==0)
cout<<"\n\tthe Entered word is palindrome";
else
cout<<"\n\tthe Entered word in not palindrome";
}
Output(12-EE-133)

Sohail Akhtar 12-EE-133 A2


OOP C++ lab#10

PROGRAM# 6
#include <iostream.h>
#include <conio.h>
void main ()
{
int a=5; int*pointer=&a;
cout<<"\n\tThe contents of a: "<<a;
cout<<"\n\tThe address of a: "<<&a;
cout<<"\n\tThe contents of a through pointer: "<<*pointer;
cout<<"\n\tThe address of a through pointer: "<<pointer;
cout<<"\n\ttThe adress of pointer: "<<&pointer;}

Output(12-EE-133)

Sohail Akhtar 12-EE-133 A2


OOP C++ lab#11

PROGRAM# 1
#include <iostream.h>

#include <conio.h>

void main ()

int i=10,j=20,*p=&i,*q=&j;

cout<<"\n\t&*&i : "<<&*&i;

cout<<"\n\t*&*&j : "<<*&*&j;

cout<<"\n\t**&p : "<<**&p;

Output(12-EE-133)

PROGRAM# 2
#include <iostream.h>

#include <conio.h>

void main()

int a[10]={1,2,3,4,5,6,7,8,9,10};

int *p;

p=a;

cout<<"*(p+2)="<<*(p+2)<<endl;

Sohail Akhtar 12-EE-133 A2


OOP C++ lab#11

cout<<"*p+3="<<*p+3<<endl;

cout<<"*(p+3)+5="<<*(p+3)+5<<endl;

cout<<"*(a+4)="<<*(a+4)<<endl;

cout<<"p[5]="<<p[5]<<endl;

cout<<"a[2]+*p="<<a[2]+*p<<endl;

getch();}

Output(12-EE-133)

PROGRAM# 3
#include <iostream.h>

#include <conio.h>

void main()

{int arr[]={2,30,14,35};

int *ptr=arr;

cout<<"++*ptr ="<<++*ptr<<endl;

cout<<"*ptr++ ="<<*ptr++<<endl;

cout<<"(*ptr)++ ="<<(*ptr)++<<endl;

cout<<"*++ptr ="<<*++ptr<<endl;

cout<<"++*ptr++ ="<<++*ptr++<<endl;

getch();

Sohail Akhtar 12-EE-133 A2


OOP C++ lab#11

Output(12-EE-133)

PROGRAM# 5
#include<iostream.h>

#include<conio.h>

void swap2(int &,int &);

void swap1(int,int);

int main ( )

int a,b;

cout<<"enter two no.\t";

cin>>a>>b;

swap1(a,b);

cout<<"a="<<a<<"\t b="<<b<<endl;

swap2(a,b);

cout<<"a="<<a<<"\t b="<<b<<endl;

return 0;

void swap1(int x,int y)

x=x+y;

y=x-y;

Sohail Akhtar 12-EE-133 A2


OOP C++ lab#11

x=x-y;

void swap2(int &x,int &y)

x+=y;

y=x-y;

x=x-y;

Output(12-EE-133)

PROGRAM# 6
#include <iostream.h>

#include <conio.h>

void func1(int &x,int &y)

{ x=100,y=200;

void main()

{int b,c;

b=20;c=40;

cout<<"b ="<<b<<endl;

Sohail Akhtar 12-EE-133 A2


OOP C++ lab#11

cout<<"c ="<<c<<endl;

func1(b,c);

cout<<"b ="<<b<<endl;

cout<<"c ="<<c<<endl;

getch();}

Output(12-EE-133)

PROGRAM# 7
#include<iostream.h>

#include<conio.h>

void func(const int &);

int main ( )

const int a[5]={5,6,7,8,9};

int b[5]={10,11,12,13,14};

func(b[0]);

func (a[0]);

return 0;

Sohail Akhtar 12-EE-133 A2


OOP C++ lab#11

void func(const int &x)

cout<<x;}

Output(12-EE-133)

Program#8
#include<iostream.h>

#include<conio.h>

void func(const int *);

int main ( )

const int a[5]={5,6,7,8,9};

int b[5]={10,11,12,13,14};

const int *p=a;

int *q=b;

func(b);

func (a);

func(q);

func(p);

return 0;

void func(const int *x)

Sohail Akhtar 12-EE-133 A2


OOP C++ lab#11

cout<<*++x<<endl;}

Output(12-EE-133)

PROGRAM# 8(a)
#include <iostream.h>

#include <conio.h>

class reference

{int a; const int b;public:

reference():b(20) {a=10;}

void func1(){cout<<"non-const function"<<endl;cout<<"a ="<<++a<<"\nb="<<++b;}

void func2()const{cout<<"constant function"<<endl;cout<<" ="<<++a<<"\nb="<<++b<<endl;}

};

void main()

{reference ref;

ref.func1();

ref.func2();

getch();}

Output(12-EE-133)

Sohail Akhtar 12-EE-133 A2


OOP C++ lab#11

PROGRAM# 9(a)
#include<iostream.h>

#include<conio.h>

int *fun(int z)

cout<<z<<endl;

z=42;

return &z;

void main()

cout<<*fun(5)<<endl;

int *P=fun(10);

cout<<++*P;

Output(12-EE-133)

PROGRAM# 9(b)
#include<iostream.h>

Sohail Akhtar 12-EE-133 A2


OOP C++ lab#11

#include<conio.h>

int & fun(int &z)

cout<<z<<endl;

z=42;

return z;

void main()

int a=5;

cout<<fun(a)<<endl;

cout<<a;

Output(12-EE-133)

PROGRAM# 10
#include<iostream.h>

#include<conio.h>

void main()

int a=1,*q=&a,**p=&q;

cout<<++a<<"\t"<<++**p<<"\t"<<++*q<<"\t"<<a<<endl;

Sohail Akhtar 12-EE-133 A2


OOP C++ lab#11

cout<<**p<<endl;

cout<<--*q<<endl;

cout<<--a<<endl;

cout<<--**p<<endl;

Output(12-EE-133)

Sohail Akhtar 12-EE-133 A2


OOP C++ lab#12

PROGRAM# 1
#include<iostream.h>

#include<conio.h>

int main ( )

int size,total=0,*array;

cout<<"enter the total number of values you want to enter: \t";

cin>>size;

array=new int[size];

for (int loop=0;loop<size;loop++)

cout<<"enter value: \t";

cin>>array[loop];

total+=array[loop];

cout<<"\n average is :\t"<<(float)total/size<<endl;

delete array;

return 0;

Output(12-EE-133)

Sohail Akhtar 12-EE-133 A2


OOP C++ lab#12

PROGRAM# 2
#include<iostream.h>

#include<conio.h>

class dummy

int a,b;

public:

void getdata();

void display();

dummy();

~dummy();

};

int main()

dummy *dumdum=new dummy;

dumdum->getdata();

(*dumdum).display();

dummy *dumdum2=new dummy[10];

for (int loop=0;loop<10;loop++)

Sohail Akhtar 12-EE-133 A2


OOP C++ lab#12

dumdum2->getdata();

(*dumdum2).display();

return 0;

void dummy::getdata()

cout<<"\nenter #:";

cin>>a;

cout<<"enter #:";

cin>>b;

void dummy::display()

cout<<"\na="<<a<<"\tb="<<b;

dummy::dummy()

a=0;

b=0;

dummy::~dummy()

{}

Sohail Akhtar 12-EE-133 A2


OOP C++ lab#12

Output(12-EE-133)

PROGRAM# 3
#include<iostream.h>

#include<conio.h>

#include<stdlib.h>

#include<string.h>

const int MAXWORD=50,SIZE=100;

void sort_words(char*[],int);

void swap(char**,char**);

int main ( )

char *wrd[SIZE],word[MAXWORD];

int n,i;

cout<<"\n Enter a string and terminate it with [ctrl+z]"<<endl;

for (i=0;cin>>word,!cin.eof();i++)

Sohail Akhtar 12-EE-133 A2


OOP C++ lab#12

if (i>SIZE)

cout<<"\nAt most"<<SIZE<<"elements can be sorted ";

exit(1);

wrd[i]=new char [strlen(word)+1];

strcpy(wrd[i],word);

n=i;

sort_words(wrd,n);

for (i=0;i<n;i++)

cout<<wrd[i]<<" ";

getch();

return 0;

void sort_words(char*w[],int n)

int i,j;

for (i=0;i<n;i++)

for (j=i+1;j<n;j++)

if (strcmp(w[i],w[j])>0)

swap(&w[i],&w[j]);

void swap(char**p,char**q)

Sohail Akhtar 12-EE-133 A2


OOP C++ lab#12

char*tmp;

tmp=*p;

*p=*q;

*q=tmp;

Output(12-EE-133)

PROGRAM# 4

#include<iostream.h>

#include<conio.h>

#include<string.h>

class String

char *s;

int l;

public:

String()

l=79;

s=new char[79+1];

Sohail Akhtar 12-EE-133 A2


OOP C++ lab#12

String(int x)

s=new char[x+1];

l=x;

String(char *st)

l=strlen(st);

s=new char[l+1];

strcpy(s,st);

void Assign(char *str)

strcpy(s,str);

l=strlen(str);

void print()

cout<<s<<endl;

int len()

return l;

Sohail Akhtar 12-EE-133 A2


OOP C++ lab#12

void concat(String &a,String &b)

delete s;

s=new char[a.l+b.l+1];

strcpy(s,a.s);

strcat(s,b.s);

l=a.l+b.l;

~String()

delete s;

};

int main()

char *q="ahmed",*p="bilal";

String S1;

cout<<"\n\t S1\n";

S1.print();

cout<<"S1 length="<<S1.len()<<endl;

cout<<"\n***********************************************************\n";

String S2(20);

cout<<"\n\t S2\n";

S2.print();

Sohail Akhtar 12-EE-133 A2


OOP C++ lab#12

cout<<"S2 length="<<S2.len()<<endl;

cout<<"\n***********************************************************\n";

String S3(q);

cout<<"\n\t S3\n";

S3.print();

cout<<"S3 length="<<S3.len()<<endl;

cout<<"\n***********************************************************\n";

S3.Assign(p);

cout<<"\n\t MODIFIED By Assign S3\n";

S3.print();

cout<<"S3 length="<<S3.len()<<endl;

cout<<"\n***********************************************************\n";

cout<<"\n\t Modified by Concat S1\n";

S1.concat(S2,S3);

S1.print();

cout<<S1.len();

getch();

return 0;

Output(12-EE-133)

Sohail Akhtar 12-EE-133 A2


OOP C++ lab#12

PROGRAM# 5
#include<iostream.h>

#include<conio.h>

class A{

protected:int i,j;

public: int k;

A():i(1),j(1){}

A(int a,int b):i(a){j=b;}

inline void show(){

cout<<"ints are"<<i<<" and "<<j<<endl;

cout<<"Public int is "<<k<<endl;}};

class B:public A{int m;

public:

B():m(0){}

B(int n):m(n){}

void sum(){

Sohail Akhtar 12-EE-133 A2


OOP C++ lab#12

cout<<"sum="<<i+j<<endl;}

inline void showm(){

cout<<"derived int is "<<m<<endl;}};

main(){

B b(10);

A a(45,65);

a.show();

b.show();

b.showm();

b.k=100;

b.show();

//b.i=10; //error

b.sum();

//a.showm(); //error

getche();

return 0;}

Output(12-EE-133)

Sohail Akhtar 12-EE-133 A2


OOP C++ lab#13

PROGRAM# 1
#include<iostream.h>

#include<conio.h>

#include<new.h>

#include<stdlib.h>

class matrix{

int **p,s1,s2;

public: int ub1,ub2;

matrix(int d1,int d2){

if(d1<0||d2<0) {

cerr<<"Illegal matrix size"<<endl;exit(1);}

s1=d1;s2=d2;

p=new int*[s1];

for(int i=0;i<s1;++i) p[i]=new int [s2];

ub1=s1-1;ub2=s2-1;}

int& element(int i,int j){

if(i<0||i>ub1||j<0||j>ub2)

{cerr<<"Illegal matrix index"<<endl;exit(1);}

return p[i][j];}

void input(){

cout<<"Enter elements";

for(int k=0;k<s1;k++)

for(int g=0;g<s2;g++)

Sohail Akhtar 12-EE-133 A2


OOP C++ lab#13

cin>>p[k][g];

cout<<endl;}

void printValues(){

cout<<"elements of matrix:\n";

for(int k=0;k<s1;k++)

{for(int g=0;g<s2;g++)

cout<<p[k][g]<<" ";

cout<<endl;}

void printAddress(){

cout<<"Adresses of elements of matrix:\n";

for(int k=0;k<s1;k++)

{for(int g=0;g<s2;g++)

cout<<&p[k][g]<<" ";

cout<<endl;}

void printScalarValues(){

cout<<"Scalar value of elements of matrix:\n";

cout<<sizeof(p)<<endl;

cout<<sizeof(*p);

cout<<endl;

~matrix(){

Sohail Akhtar 12-EE-133 A2


OOP C++ lab#13

for(int i=0;i<=ub1;++i)delete p[i];

delete p;}

};

main(){

matrix a(3,3);

a.input();

a.printValues();

a.printAddress();

a.printScalarValues();

getche();

return 0;}

Output(12-EE-133)

PROGRAM# 2
#include<iostream.h>

Sohail Akhtar 12-EE-133 A2


OOP C++ lab#13

#include<conio.h>

#include<new.h>

struct link

int data;

link* next;};

class Linklist

link* first;public:

Linklist(){first=NULL;}

void additem(int d){

link* newlink=new link;

newlink->data=d;

newlink->next=first;

first=newlink;

void display(){

link* current=first;

while(current!=NULL){cout<<current->data<<endl;

current=current->next;}

};

int main(){

Linklist a;

a.additem(25);

Sohail Akhtar 12-EE-133 A2


OOP C++ lab#13

a.additem(35);

a.additem(45);

a.additem(55);

a.display();

getche();

Output(12-EE-133)

PROGRAM# 3
#include<iostream.h>

#include<conio.h>

class A{

int x;

protected:int y;

public:

A(int m=0,int n=0):x(m),y(n){}

void show(){

cout<<"\nprivate int of A:"<<x<<"\npublic int of A:"<<y<<endl;}};

class B:public A{

int z;public:

B(int i=0,int j=0,int k=0):A(i,j),z(k){}

void show(){cout<<"\nprotected int of A:"<<y<<"\nprivate int of B:"<<z<<endl;}};

Sohail Akhtar 12-EE-133 A2


OOP C++ lab#13

int main(){

A a=A(1,2);

B b(3,4,5);

a.show();

b.show();

b.A::show();

getche();

return 0;}

Output(12-EE-133)

PROGRAM# 4
#include<iostream.h>

#include<conio.h>

class B;

class A{int a;public:

A(int x=0):a(x){}

friend int sum(A,B);};

class B{int b;

public:

B(int y=0):b(y){} friend int sum(A,B);};

Sohail Akhtar 12-EE-133 A2


OOP C++ lab#13

int sum(A i,B j)

{ return i.a+j.b;}

int main(){

A a(5);

B b(6);

cout<<"Sum of private ints of classes A and B="<<sum(a,b)<<endl;

getch();

return 0;

Output(12-EE-133)

PROGRAM# 5
#include<iostream.h>

#include<conio.h>

class web{

int x,y;

public:

web():x(0),y(0){}

web(int a,int b){x=a;y=b;}

void operator++(int){

++x;++y;}

void operator++(){

++x;++y;}

Sohail Akhtar 12-EE-133 A2


OOP C++ lab#13

web operator+(web a){

return web(x+a.x,y+a.y);}

web operator-(web a){

return web(x-a.x,y-a.y);}

web operator*(web a){

return web(x*a.x,y*a.y);}

web operator/(web a){

return web(x/a.x,y/a.y);}

void show(){

cout<<"x="<<x<<"\ny="<<y<<endl;}

};

int main(){ //clrscr();

web ob1,ob2(2,4),ob3(2,6);

ob3++;

++ob3;

ob1=ob2+ob3;

ob1.show();

ob3=ob1/ob2;

ob3.show();

web ob4(10,20);

ob2=(ob1*ob3)-ob4;

ob2.show();

getche();

return 0;}

Output(12-EE-133)
Sohail Akhtar 12-EE-133 A2
OOP C++ lab#13

Sohail Akhtar 12-EE-133 A2


OOP C++ lab#14

PROGRAM# 1
#include <iostream.h>

#include <conio.h>

#include <string.h>

void main( int arg, char* argv[] )

{ argv[0] = "today";

argv[1] = "is ";

argv[2] = "a";

argv[3] = "very";

argv[4] = "nice";

argv[5] = "day";

arg=6;

for(int i = 0 ; i <arg ; i++) cout<<"\n\t"<<*argv++<<endl;

getch();

Output(12-EE-133)

Sohail Akhtar 12-EE-133 A2


OOP C++ lab#14

PROGRAM# 2(a)
#include <iostream.h>

#include <conio.h>

const int EXIT_SUCCESS = 0;

class B

{ char* ptrB; public:

B(){ptrB = new char[5]; cout << "\n\tB allocates 5 bytes "<<endl;}

~B() { delete [] ptrB; cout<< "\n\tB de-allocates 5 bytes "<<endl; }

};

class D: public B

{ char* ptrD; public:

D( ){ptrD = new char[10]; cout<<"\n\tD allocates 10 bytes"<<endl;}

~D(){ delete[] ptrD; cout<< "\n\tD de-allocates 10 bytes"<<endl;}

};

void f()

{ B* p=new B;

delete p;

Sohail Akhtar 12-EE-133 A2


OOP C++ lab#14

int main()

{ f();

getche(); return EXIT_SUCCESS;}

Output(12-EE-133) 1

Output(12-EE-133) 2

PROGRAM# 2(b)
#include <iostream.h>

class B1 {protected: int x;};

class B2 {protected: int x;};

class D: public B1, public B2

{protected: int x;

public:

void test()

{x=999; B1::x=111; B2::x=222;

cout<<"\n\td.x="<<x<<"\n\tB1::x="<<B1::x<<"\n\tB2::x="<<B2::x<<endl;} };

void main (void)

{D d;

Sohail Akhtar 12-EE-133 A2


OOP C++ lab#14

d.test();

Output(12-EE-133)

PROGRAM# 3
#include <iostream.h>

#include <string.h>

class base1

{ protected: char message[80]; public:

base1(char* m) { strcpy(message,m);}

char* show1() {return message;} };

class base2: public base1

{ protected: char message[80]; public:

base2(char* m):base1("Default Value") {strcpy(message,m);}

base2(char* m,char* n):base1(n) {strcpy(message,m);}

char* show2(){return message;} };

class base3:public base2

{ char message[80]; public:

base3(char* p,char* q,char* r):base2(q) {strcpy(message,p); strcpy(base1::message,r);}

char* show3() {return message;} };

void main()

{base1 b1("How are you?");

base2 b2 ("I am fine");

Sohail Akhtar 12-EE-133 A2


OOP C++ lab#14

base2 b3("what is your name?","My name is XYZ");

base3 b("Where do you live?","I live in Tazila","Very good place");

cout<<"b1::base1= "<<b1.show1()<<endl;

cout<<"b2::base2= "<<b2.show2()<<endl;

cout<<"b2::base1= "<<b2.show1()<<endl;

cout<<"b3::base2= "<<b3.show2()<<endl;

cout<<"b::base3= "<<b.show3()<<endl;

cout<<"b::base2= "<<b.show2()<<endl;

cout<<"b::base1="<<b.show1()<<endl;

Output(12-EE-133)

Sohail Akhtar 12-EE-133 A2

You might also like