Docsity

C++ Programming: Defining and Modifying Classes with Friend Functions, Assignments of Data Structures and Algorithms

Toggle sidebar
bg1
#include<iostream>
using namespace std;
class Box{
double width;
public:
friend void printWidth(Box box);
void setWidth(double wid);
};
void Box :: setWidth(double wid){
width = wid;
}
void printWidth(Box box){
box.width = box.width * 2;
cout<<"Width of box :"<<box.width<<endl;
}
int main(){
Box box;
box.setWidth(10.0);
printWidth(box);
return 0;
}

Partial preview of the text

Download C++ Programming: Defining and Modifying Classes with Friend Functions and more Assignments Data Structures and Algorithms in PDF only on Docsity!

#include using namespace std; class Box{ double width; public: friend void printWidth(Box box); void setWidth(double wid); }; void Box :: setWidth(double wid){ width = wid; } void printWidth(Box box){ box.width = box.width * 2; cout<<"Width of box :"<<box.width<<endl; } int main(){ Box box; box.setWidth(10.0); printWidth(box); return 0; }