-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClasses.cpp
More file actions
56 lines (51 loc) · 1.69 KB
/
Copy pathClasses.cpp
File metadata and controls
56 lines (51 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include<bits/stdc++.h>
using namespace std;
//Class is a blue print of a data type;
//eg BOX is blue print of data type having box length ,breadth, hieght it does not include any function;
//Class have three type of members
// private members of a class are accessible only from within other members of the same class (or from their "friends").
// protected members are accessible from other members of the same class (or from their "friends"), but also from members of their derived classes.
// Finally, public members are accessible from anywhere where the object is visible.
class BOX{
public:
int length;
int breadth;
int hieght;
};
//Class Rectangle having function set_values() and area() and two private member width and breadth;
class Rectangle{
int width,breadth;
public:
void set_values(int,int); //Prototype of function in class
int area(){return width*breadth;}
};
// Declaring function
void Rectangle::set_values(int x,int y){
width = x;
breadth=y;
}
//set_values function can easily be avoid by using constructor
//Constructor is nothing but is a function member of class having none of return type even not void and same name as class
//Let's make Triangle using constructor
class Triangle{
int base,prep,hyp;
public:
Triangle(int,int,int); //Constructor
int area(){return base*prep*hyp;}
};
Triangle::Triangle(int x, int y, int z){
base = x;
prep = y;
hyp = z;
}
int main(){
BOX box1;
cin>>box1.length>>box1.breadth>>box1.hieght;
cout<<box1.length<<" "<<box1.breadth<<" "<<box1.hieght<<endl;
cout<<"Volume of box1 is: "<<box1.length * box1.breadth * box1.hieght<<endl;
Rectangle rect;
rect.set_values(4,5);
cout<<rect.area()<<endl;
Triangle tri(3,4,5);
cout<<tri.area()<<endl;
}