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

C++ assignment 4

The document contains C++ code examples demonstrating the use of 'this' pointers in classes, including a Box class for volume comparison and an Employee class for data member assignment. It also includes an example of dynamic object creation with a class named 'item' that manages an array of items. The code illustrates input, output, and memory management in C++.
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)
3 views

C++ assignment 4

The document contains C++ code examples demonstrating the use of 'this' pointers in classes, including a Box class for volume comparison and an Employee class for data member assignment. It also includes an example of dynamic object creation with a class named 'item' that manages an array of items. The code illustrates input, output, and memory management in C++.
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/ 9

13.

Wap in C++ to implement this poiner

A,calling function using this pointers

B Calling data members using this pointers

c. calling function using BOX object this pointer

14. wap in c++ to implement dynamic object


13. A

#include <iostream.h>

class Box {

public:

void input(double l , double b, double h)

length = l;

breadth = b;

height = h;

double Volume() {

return length * breadth * height;

int compare(Box box) {

return this->Volume() > box.Volume();

private:

double length; // Length of a box

double breadth; // Breadth of a box

double height; // Height of a box

};

int main(void) {
Box b1; // Declare b1

Box b2; // Declare b2

b1.input(3.3, 1.2, 1.5);

b2.input(8.5, 6.0, 2.0);

if(b1.compare(b2)) {

cout << "Box2 is smaller than Box1" <<endl;

} else {

cout << "Box2 is equal to or larger than Box1" <<endl;

return 0;

}
13.B

#include<iostream.h>

#include<string.h>

class Employee {

public:

int id;//data member(also instance variable)

char *name;//data member(also instance variable)

float salary;

void getdata(int id, char *name,float salary)

this->id=id;

this->name=name;

this->salary=salary;

void display()

cout<<id<<" "<<name<<" "<<salary<<" "<<endl;

};

int main(void)

Employee e1;//creating an object of Employee

Employee e2; //creating an object of Employee

e1.getdata(101,"Sonno",89000);
e2.getdata(102,"Vicky",59000);

e1.display();

e2.display();

return 0;

}
13 c.

#include <iostream.h>

class Box {

public:

void input(double l , double b, double h)

length = l;

breadth = b;

height = h;

double Volume() {

return length * breadth * height;

int compare(Box box) {

return this->Volume() > box.Volume();

private:

double length; // Length of a box

double breadth; // Breadth of a box

double height; // Height of a box

};

int main(void) {
Box b1; // Declare b1

Box b2; // Declare b2

b1.input(3.3, 1.2, 1.5);

b2.input(8.5, 6.0, 2.0);

if(b1.compare(b2)) {

cout << "Box2 is smaller than Box1" <<endl;

} else {

cout << "Box2 is equal to or larger than Box1" <<endl;

return 0;

}
14.

#include<iostream.h>

#include<conio.h>

class item

int code;

float price;

public:

void getdata(int a,int b)

code=a;

price=b;

void show()

cout<<"\nCode is: "<<code;

cout<<"\nPrice is: "<<price;

};

const int size=2;

int main()

clrscr();

item *p=new item[size];


item *d=p;

int x,i;

float y;

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

cout<<"\nInput code and price for item"<<i+1<<" ";

cin>>x>>y;

p->getdata(x,y);

p++;

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

cout<<"\nItem:"<<i+1;

d->show();

d++;

delete [] p;

return 0;

You might also like