C++ Programme
C++ Programme
#include <iostream>
class Box {
private:
double length, width; // Private members
public:
// Constructor to initialize the values
Box(double l, double w) {
length = l;
width = w;
}
double getWidth() {
return width;
}
};
int main() {
double l, w;
return 0;
}
#include <iostream>
using namespace std;
class Box {
protected:
double width; // Protected member
public:
Box(double w) : width(w) {} // Constructor
};
void printWidth() {
cout << "Width of the box: " << width << endl;
}
};
int main() {
double w;
cout << "Enter the width of the box: ";
cin >> w;
DerivedBox myBox(w);
myBox.printWidth();
return 0;
}
#include <iostream>
using namespace std;
class Example {
public:
int pubVar = 10;
private:
int priVar = 20;
protected:
int proVar = 30;
public:
void show() {
cout << "Public: " << pubVar << ", Private: " << priVar << ", Protected: "
<< proVar << endl;
}
};
int main() {
Example obj;
obj.pubVar = 50; // Allowed
// obj.priVar = 60; // Not Allowed
// obj.proVar = 70; // Not Allowed
obj.show();
Derived d;
d.showDerived();
return 0;
}