1#include <iostream>
2using namespace std;
3
4class Foo
5 {
6 public:
7 Foo ( )
8 {
9 cout << "constructor Foo\n";
10 }
11};
12
13class Bar
14 {
15 public:
16 Bar ( Foo )
17 {
18 cout << "constructor Bar\n";
19 }
20};
21
22int main()
23{
24 /* 1 */ Foo* foo1 = new Foo ();
25 /* 2 */ Foo* foo2 = new Foo;
26 /* 3 */ Foo foo3;
27 /* 4 */ Foo foo4 = Foo::Foo();
28
29 /* 5 */ Bar* bar1 = new Bar ( *new Foo() );
30 /* 6 */ Bar* bar2 = new Bar ( *new Foo );
31 /* 7 */ Bar* bar3 = new Bar ( Foo foo5 );
32 /* 8 */ Bar* bar3 = new Bar ( Foo::Foo() );
33
34 return 1;
35}
36
1class Rectangle
2{
3 int width, height;
4public:
5 void set_values (int,int);
6 int area() {return width*height;}
7};
8
9void Rectangle::set_values (int x, int y)
10{
11 width = x;
12 height = y;
13}
1class Exemple
2{
3 private:
4 /* data */
5 public:
6 Exemple(); //Constructor
7 ~Exemple(); //Destructor
8};
1#include <iostream>
2
3using namespace std;
4
5class Box {
6 public:
7 double length; // Length of a box
8 double breadth; // Breadth of a box
9 double height; // Height of a box
10
11 // Member functions declaration
12 double getVolume(void);
13 void setLength( double len );
14 void setBreadth( double bre );
15 void setHeight( double hei );
16};
17
18// Member functions definitions
19double Box::getVolume(void) {
20 return length * breadth * height;
21}
22
23void Box::setLength( double len ) {
24 length = len;
25}
26void Box::setBreadth( double bre ) {
27 breadth = bre;
28}
29void Box::setHeight( double hei ) {
30 height = hei;
31}
32
33// Main function for the program
34int main() {
35 Box Box1; // Declare Box1 of type Box
36 Box Box2; // Declare Box2 of type Box
37 double volume = 0.0; // Store the volume of a box here
38
39 // box 1 specification
40 Box1.setLength(6.0);
41 Box1.setBreadth(7.0);
42 Box1.setHeight(5.0);
43
44 // box 2 specification
45 Box2.setLength(12.0);
46 Box2.setBreadth(13.0);
47 Box2.setHeight(10.0);
48
49 // volume of box 1
50 volume = Box1.getVolume();
51 cout << "Volume of Box1 : " << volume <<endl;
52
53 // volume of box 2
54 volume = Box2.getVolume();
55 cout << "Volume of Box2 : " << volume <<endl;
56 return 0;
57}
1
2 class MyClass {
3 // The class
4 public:
5 // Access specifier
6 int myNum; //
7 Attribute (int variable)
8 string myString; //
9 Attribute (string variable)
10};
11
12