Loading....
Coupon Accepted Successfully!

 

More Programs

13.29 Write a program to declare the array of class objects. Use constructors and destructors. Read names and display with their length.

#include<iostream.h>

#include<conio.h>

#include<string.h>

#include<iomanip.h>

class word

{

private :

char name[20];

int len;

public :

word()

{

cout<<“\nEnter Name : ”;

cin.getline(name,20);

len=strlen(name);

}

~word()

{

cout<<name <<“(“<<len<<”)\n”;

}

};

int main()

{

clrscr();

word w[5];

cout<<“\nEntered names with their length \n\n”;

return 0;

}

OUTPUT
Enter Name : Sourav
Enter Name : Sachin
Enter Name : Rahul
Enter Name : Ajay
Enter Name : Ajit
Entered names with their length
Ajit(4)
Ajay(4)
Rahul(5)
Sachin(6)
Sourav(6)
 
Explanation: In the above program, the class word has one data member of character-type array. The class also contains a constructor and a destructor. In function main(), an array of five objects is defined. The constructor executes five times, and names are entered through the keyboard. The constructor also calculates the length of the entered string. The destructor displays the string and its length.
 

13.30 Write a program to create array of objects. Initialize and display them using non-member function.

#include<iostream.h>

#include<conio.h>

class A

{

public:

char c;

int i;

float f;

A()

{

cout<<“\n Enter char, int and float value ”;

cin>>c>>i>>f;

}

};

int main()

{

void out(A);

clrscr();

A a[]={A(),A()};

out(a[0]);

out (a[1]);

return 0;

}

void out (A c)

{

cout<<“\n char value = ”<<c.c;

cout<<“\n int value = ”<<c.i;

cout<<“\n float value = ”<<c.f;

cout<<“\n”;

}

OUTPUT
Enter char, int and float value T 2 2.5
Enter char, int and float value J 4 3.7
char value = T
int value = 2
float value = 2.5
char value = J
int value = 4
float value = 3.7

Explanation:
In the above program, class A holds three elements c, i, and f of char, int, and float data type. The class A also has a constructor that is used for the initialization of objects. The initialization of objects is nothing but assigning value to the associated member elements of the class. In main() function array of object is declared. The constructor of the class is invoked and values to the member variables are assigned. The class A is defined before function main(). Another non-member function out() is defined for displaying the contents of the object.
 
Array ­elements are passed to the function out(), and contents are displayed. The out() ­function has one argument of type class A. The argument receives the copy of the contents of array ­objects.
 

13.31 Write a program to pass this pointer as argument to the member function and display the contents.

#include<iostream.h>

#include<conio.h>

class A

{

private:

char c;

int i;

float f;

public:

A()

{

cout<<“\n Enter char, int and float value ”;

cin>>c>>i>>f;

out(this);

}

void out (A *a )

{

cout<<“\n char value = ”<<a->c;

cout<<“\n int value = ”<<a->i;

cout<<“\n float value = ”<<a->f;

cout<<“\n”;

}

};

int main()

{

clrscr();

A a;

return 0;

}

OUTPUT

Enter char, int and float value F 2 2.8

char value = F

int value = 2

float value = 2.8


Explanation:
In the above program, the pointer this is passed to the member function out(). In function out() using -> operator, the contents of elements are displayed. We know that the this pointer is always present in every member function and it points to the calling object.
 

13.32 Write a program to assign value using this pointer.

#include<iostream.h>

#include<conio.h>

class data

{

private :

int j;

public:

void setv (int v)

{

j=v;

cout<<“\n j = ”<<j;

this->j=++v;

cout<<“\n j = ”<<j;

}

};

int main()

{

clrscr();

data d;

d.setv(7);

return 0;

}

OUTPUT

j = 7

j = 8


Explanation:
In the above program, the class data have only one element, that is, int j. The member function setv() is used to assign the value to the data element. The value passed to the setv() function is assigned to the member variable directly and using this pointer. Before assigning the value using this pointer, the variable v is incremented. The values of j in two assignments are given in the output.
 

13.33 Write a program to enter integer numbers, count them, and make fragments of equal lengths. The fragment length to be entered by the user.

 

Tip: In case the total number of elements is odd, the last segment contains the remaining elements.

 

#include<stdio.h>

#include<conio.h>

#include<iostream.h>

main()

{

int *p,*k,s=1;

int j,c=0,f,i=0;

clrscr();

k=p;

printf (“\n Enter numbers (0) to exit :”);

while (1)

{

cin>>*p;

if (*p==0) break;

c++;

p++;

}

cout<<endl<<c <<“ elements are entered ”;

cout<<endl<<“ Enter length of fragment : ”;

cin>>f;

cout<<endl<<“Fragment ”<<s<<“ :”;

for (j=0;j<c;j++)

{

i++;

cout<<“ ”<<*(k+j);

if (i==f)

{

cout<<“ ”;

s++;

cout<<“\nFragment ”<<s<<“ :”;

i=0;

}

}

return 0;

}

OUTPUT
Enter numbers (0) to exit :4 5 2 1 3 4 8 7 9 0
9 elements are entered
Enter length of fragment : 2
Fragment 1 : 4 5
Fragment 2 : 2 1
Fragment 3 : 3 4
Fragment 4 : 8 7
Fragment 5 : 9
 
Explanation: In the above program, pointers p, k and integer variables s, j, c, f, and i are declared. The integer variable s is initialized to one, and i is initialized to zero. The address of pointer p is assigned to k. Using while loop and pointer p, numbers are read through the keyboard. The pointer p is incremented in every iteration, and it points to successive memory locations. The numbers entered are stored in these memory locations. The if statement checks the entered number and if it is zero, control comes out of the loop. The variable c counts the number of elements entered.
 
The number of elements to be stored in each segment is to be entered through the keyboard. Variable f holds the number of total elements. The for loop executes from zero to c-1. The pointer k and loop variable j are used to access successive memory locations and the elements stored in them from the starting address. The statement cout<<“ ”<<*(k+j) displays elements on the screen.
 

13.34 Write a program to assign different types of data to void pointers. Display the data using void pointers on the screen using typecast syntaxes according to the data type.

#include<iostream.h>

#include<conio.h>

#define SIZE 25

void show ( void *vp, char status)

{

switch(status)

{

case ‘l’:

cout<<“\n The long integer entered : ”<<*((long*)vp);

delete vp;

break;

case ‘i’:

cout<<“\n The integer entered : ”<<*((int*)vp);

delete vp;

break;

case ‘s’ :

cout<<“The entred string : ”<<(char*) vp;

delete vp;

break;

case ‘f’:

cout<<“\n The float entered : ”<<*((float*)vp);

delete vp;

break;

}

}

void main()

{

clrscr();

int *i;

char *s;

float *f;

long *l;

char choice;

cout<<“\n Enter data type (s)tring, (i)nt, (f)loat(l)ong:”;

cin.get(choice);

switch(choice)

{

case ‘l’ :

l=new long;

cout<<“\n Enter an long integer : ”;

cin>>*l;

show(l,choice);

break;

case ‘s’:

s=new char[SIZE];

cout<<“\n Enter a string :”;

cin>>s;

show(s,choice);

break;

case ‘i’ :

i=new int;

cout<<“\n Enter an integer : ”;

cin>>*i;

show(i,choice);

break;

case ‘f’ :

f= new float;

cout<<“\n Enter a float : ”;

cin>>*f;

show (f,choice);

break;

default :

cout<<“\n Invalid choice ” ;

}

}

OUTPUT
Enter data type (s)tring, (i)nt, (f)loat (l)ong :l
Enter an long integer : 52114
The long integer entered : 52114
 
Explanation: In the above program, the pointers i, s, f, and l of int, char, float, and long type are declared, respectively. The character variable choice is declared. The program prompts the user to enter his/her choice. The switch() case executes the appropriate case according to the user choice. By using new operators, memory is allocated to the respective pointers and again, the program prompts the user to enter data. The data and choice entered by the user are passed to function show(). The function show() contains void pointers. In function show(), the type of void pointer is changed to char, long, int, and float. The type depends on the value of variable choice. Thus, the entered data are displayed.




Test Your Skills Now!
Take a Quiz now
Reviewer Name