63 151
Assignments Done
99,1%
Successfully Done
In July 2018

Answer to Question #5559 in C++ for Jimmy

Question #5559
write a c++ to give an appropriate example of a multiple inheritance
Expert's answer
class A{ //parent класс
};

class B : public A{ //public
inheritance
};

class C : protected A{ //protected
inheritance
};

class Z : private A{ //private
inheritance
};

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

Assignment Expert
15.12.11, 13:07

1: // Demonstrates declaration of a constructors and
2: // destructor for the Cat class
3:
4: #include <iostream.h> // for cout
5:
6: class Cat // begin declaration of the class
7: {
8: public: & // begin public section
9: Cat(int initialAge); & // constructor
10: & ~Cat(); // destructor
11: & int GetAge(); // accessor function
12: & void SetAge(int age); // accessor function
13: & void Meow();
14: private: // begin private section
15: & int itsAge; & // member variable
16:& };
17:
18:& // constructor of Cat,
19:& Cat::Cat(int initialAge)
20:& {
21: & itsAge = initialAge;
22:& }
23:
24:& Cat::~Cat() & // destructor, takes no action
25:& {
26:& }
27:
28:& // GetAge, Public accessor function
29:& // returns value of itsAge member
30:& int Cat::GetAge()
31:& {
32: & return itsAge;
33:& }
34:
35:& // Definition of SetAge, public
36:& // accessor function
37:
38:& void Cat::SetAge(int age)
39:& {
40: & // set member variable its age to
41: & // value passed in by parameter age
42: & itsAge = age;
43:& }
44:
45:& // definition of Meow method
46:& // returns: void
47:& // parameters: None
48:& // action: Prints "meow" to screen
49:& void Cat::Meow()
50:& {
51: & cout << "Meow.\n";
52:& }
53:
54:& // create a cat, set its age, have it
55 // meow, tell us its age, then meow again.
56:& int main()
57:& {
58: Cat Frisky(5);
59: Frisky.Meow();
60: cout << "Frisky is a cat who is " ;
61: cout << Frisky.GetAge() << " years old.\n";
62: Frisky.Meow();
63: Frisky.SetAge(7);
64: cout << "Now Frisky is " ;
65: cout << Frisky.GetAge() << " years old.\n";
66; & return 0;
67: }

Output: Meow.
Frisky is a cat who is 5 years old.
Meow.
Now Frisky is 7 years old.

Analysis: The above program adds a constructor that takes an integer. Line 10 declares the destructor, which takes no parameters. Destructors never take parameters, and neither constructors nor destructors return a value--not even void.
Lines 19-22 show the implementation of the constructor. It is similar to the implementation of the SetAge() accessor function. There is no return value.

Lines 24-26 show the implementation of the destructor ~Cat(). This function does nothing, but you must include the definition of the function if you declare it in the class declaration.

Line 58 contains the definition of a Cat object, Frisky. The value 5 is passed in to Frisky's constructor. There is no need to call SetAge(), because Frisky was created with the value 5 in its member variable itsAge, as shown in line 61. In line 63, Frisky's itsAge variable is reassigned to 7. Line 65 prints the new value.

Jimmy
15.12.11, 09:03

sir please help me in this
c++ program to give an example of constructor and destructor

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions