You are on page 1of 19

Basic class

#include <iostream>

using namespace std;

////////////////////////////////////////////////

class Interval

public:

void SetTime(long value) //void Interval::SetTime(Interval* this, long value)

minutes = value / 60; //this->minutes = value / 60;

seconds = value % 60; //this->seconds = value % 60;

Long GetTime()

return 60 * minutes + seconds;

void Print()

if(seconds < 10)

cout<< minutes << ":0" << seconds <<endl;


else

cout<< minutes << ":" << seconds <<endl;

private:

long minutes;

long seconds;

};

////////////////////////////////////////////////

Float GetSpeed(float distance, Interval duration)

return 3.6 * distance / duration.GetTime();

int main(void)

Interval jack;

jack.SetTime(125); //Interval::SetTime(&jack, 125)

cout<< "Jack's interval = ";

jack.Print();

cout<< "Jack's speed = " <<GetSpeed(500, jack) <<endl;

Interval jeff;

jeff.SetTime(200); //Interval::SetTime(&jeff, 200)


cout<< "Jeff's interval = ";

jeff.Print();

cout<< "Jeff's speed = " <<GetSpeed(500, jeff) <<endl;

==========================================================================

2 Classs using nly time………………………………………………………………………………………………..

#include <iostream>

using namespace std;

////////////////////////////////////////////////

class Interval

public:

void SetTime(long value)

time = value;

Long GetTime()

{
return time;

void Print()

long minutes = time / 60;

long seconds = time % 60;

if(seconds < 10)

cout<< minutes << ":0" << seconds <<endl;

else

cout<< minutes << ":" << seconds <<endl;

private:

long time;

};

////////////////////////////////////////////////

Float GetSpeed(float distance, Interval duration)

return 3.6 * distance / duration.GetTime();

int main(void)
{

Interval jack;

jack.SetTime(125);

cout<< "Jack's interval = ";

jack.Print();

cout<< "Jack's speed = " <<GetSpeed(500, jack) <<endl;

Interval jeff;

jeff.SetTime(200);

cout<< "Jeff's interval = ";

jeff.Print();

cout<< "Jeff's speed = " <<GetSpeed(500, jeff) <<endl;

========================================================================
3 Constructors…………………………………………………………………………………………………….

#include <iostream>

using namespace std;

////////////////////////////////////////////////

class Interval

public:

//default (parameterless) constructor

Interval()

time = 0;

//conversion (single-parameter) constructor

Interval(long t)

time = t;

//parameterized constructor
Interval(long s, long m)

time = 60 * m + s;

Void SetTime(long value)

time = value;

Long GetTime()

return time;

void Print()

long minutes = time / 60;

long seconds = time % 60;

if(seconds < 10)

cout<< minutes << ":0" << seconds <<endl;

else

cout<< minutes << ":" << seconds <<endl;

}
private:

long time;

};

////////////////////////////////////////////////

void Run(void)

Interval john(10, 4); //activation using parameterized constructor

cout<< "John's interval = ";

john.Print();

int main(void)

Interval jack; //activation using default constructor

jack.SetTime(125);

cout<< "Jack's interval = ";

jack.Print();

Run();

Interval jeff = 200; //activation using conversion constructor

cout<< "Jeff's interval = ";

jeff.Print();
}

=======================================================================

4 ConstrDestr…………………………………………………………

#include <iostream>

using namespace std;

////////////////////////////////////////////////

class Interval

public:

Interval(long s=0, long m=0)

time = 60 * m + s;

++count;

Void SetTime(long value)

time = value;

}
Long GetTime()

return time;

//instance method - it must be invoked on an object

//receives (hidden) this argument which points to the object on which it is invoked

//so can access non-static members of the class

void Print()

long minutes = time / 60;

long seconds = time % 60;

if(seconds < 10)

cout<< minutes << ":0" << seconds <<endl;

else

cout<< minutes << ":" << seconds <<endl;

//class method - must be invoked on the class

//does not receive this argument

//so it cannot access non-static members of the class

Static int GetCount()

return count;
}

//destructor

~Interval()

count--;

private:

long time; //instance field - each object contains its own value for this field

static int count; //class field - its value is shared by all the objects

};

int Interval::count = 0; //allocating memory for count field of Interval class

////////////////////////////////////////////////

void Run(void)

cout<< "Allocating memory for john's Interval" <<endl;

Interval john(10, 4);

cout<< "John's interval = ";

john.Print();

cout<< "Deallocating memory of john's Interval" <<endl;

}
int main(void)

cout<< "Allocating memory for jack's Interval" <<endl;

Interval jack; //Interval jack(0, 0)

jack.SetTime(125);

cout<< "Jack's interval = ";

jack.Print();

Run();

cout<< "Allocating memory for jeff's Interval" <<endl;

Interval jeff = 200; //Interval jeff(200, 0)

cout<< "Jeff's interval = ";

jeff.Print();

cout<< "Number of Intervals in memory: "

<< Interval::GetCount()

<<endl;

cout<< "Deallocating memory of jeff's Interval" <<endl;

cout<< "Deallocating memory of jack's Interval" <<endl;

===================================================================
5 ConstrDestrr use of const…………………
#include <iostream>

using namespace std;

////////////////////////////////////////////////

class Interval

public:

Interval(long s=0, long m=0)

time = 60 * m + s;

++count;

cout<< "Interval constructor invoked" <<endl;

voidSetTime(long value)

time = value;

}
longGetTime() const//long Interval::GetTime(const Interval* this)

return time; //return this->time

void Print() const

long minutes = time / 60;

long seconds = time % 60;

if(seconds < 10)

cout<< minutes << ":0" << seconds <<endl;

else

cout<< minutes << ":" << seconds <<endl;

staticintGetCount()

return count;

~Interval()

count--;

cout<< "Interval destructor invoked" <<endl;


}

private:

long time;

staticint count;

};

int Interval::count = 0;

////////////////////////////////////////////////

Float GetSpeed(float distance, const Interval& duration)

return 3.6 * distance / duration.GetTime();

int main(void)

Interval jack;

jack.SetTime(125);

cout<< "Jack's interval = ";

jack.Print();

cout<< "Jack's speed = " <<GetSpeed(500, jack) <<endl;

Interval jeff = 200;

cout<< "Jeff's interval = ";


jeff.Print();

cout<< "Jeff's speed = " <<GetSpeed(500, jeff) <<endl;

cout<< "Number of Intervals in memory: "

<< Interval::GetCount()

<<endl

=========================================================================

Commands………..
1 cd Foundations/C++/

2 mkdir Classes

3 cd Classes/

4 vi classtest1.cpp

5 c++ -o classtest1 classtest1.cpp

6 vi classtest1.cpp

7 c++ -o classtest1 classtest1.cpp

8 ./classtest1

9 vi classtest1.cpp

10 c++ -o classtest1 classtest1.cpp

11 ./classtest1

12 vi classtest1.cpp

13 c++ -o classtest1 classtest1.cpp

14 ./classtest1

15 cp classtest1.cpp classtest2.cpp

16 vi classtest2.cpp
17 c++ -o classtest2 classtest2.cpp

18 ./classtest2

19 cp classtest2.cpp ctortest.cpp

20 vi ctortest.cpp

21 c++ -o ctortest ctortest.cpp

22 ./ctortest

23 vi ctortest.cpp

24 c++ -o ctortest ctortest.cpp

25 ./ctortest

26 vi ctortest.cpp

27 c++ -o ctortest ctortest.cpp

28 ./ctortest

29 vi ctortest.cpp

30 c++ -o ctortest ctortest.cpp

31 vi ctortest.cpp

32 c++ -o ctortest ctortest.cpp

33 ./ctortest

34 vi ctortest.cpp

35 c++ -o ctortest ctortest.cpp

36 vi ctortest.cpp

37 c++ -o ctortest ctortest.cpp

38 ./ctortest

39 vi ctortest.cpp

40 c++ -o ctortest ctortest.cpp

41 ./ctortest
42 cp ctortest.cpp dtortest.cpp

43 vi dtortest.cpp

44 c++ -o dtortest dtortest.cpp

45 ./dtortest

46 vi dtortest.cpp

47 c++ -o dtortest dtortest.cpp

48 ./dtortest

49 vi dtortest.cpp

50 c++ -o dtortest dtortest.cpp

51 ./dtortest

52 vi dtortest.cpp

53 c++ -o dtortest dtortest.cpp

54 ./dtortest

55 vi dtortest.cpp

56 c++ -o dtortest dtortest.cpp

57 vi dtortest.cpp

58 cp dtortest.cpp objreftest.cpp

59 vi objreftest.cpp

60 c++ -o objreftest objreftest.cpp

61 ./objreftest

62 vi objreftest.cpp

63 c++ -o objreftest objreftest.cpp

64 ./objreftest

65 vi objreftest.cpp

66 c++ -o objreftest objreftest.cpp


67 ./objreftest

68 vi objreftest.cpp

69 c++ -o objreftest objreftest.cpp

70 vi objreftest.cpp

71 c++ -o objreftest objreftest.cpp

72 ./objreftest

73 vi objreftest.cpp

74 c++ -o objreftest objreftest.cpp

75 vi objreftest.cpp

76 vi dtortest.cpp

77 history>cmd09-04-15.txt

You might also like