Class methods vs class members? - c++

My knowledge of C++ is limited as I've only started using it about 8 months ago. Recently I've received a review comment on my code that states:
"The header doesn't define all class methods before members in the public section"
Sounds simple, or so I thought. Until I realized I don't quite know the theory behind this. What is class method vs members?
Am I right in understanding that int a is a class member, while double doubleToInt(int a) is a class method?

Related

What is a concrete class in C++? [duplicate]

This question already has answers here:
What is the difference between a concrete class and an abstract class?
(9 answers)
Closed 5 years ago.
I am in the process of reading "The C++ Programming Language 4th Edition" and I've gotten to the point where Bjarne is trying to explain concrete classes. His explanation is confusing me, and I cannot find any explanation online that satisfies me. I understand that abstract classes are classes that have at least one virtual function and that concrete classes are the opposite, but I can not wrap my head around concrete classes. The book "Programming Principles and Practice using C++" is saying that a concrete class is essentially a derived class, and an abstract class is a base class. Is this the correct interpretation? I have been trying to figure out this concept all day. Also, "a class that can be used to create objects is a concrete class". Does this mean that a class is concrete if I can do something like "myclass classobject1;", and I am not able to make objects with abstract classes?
Essentially, a concrete class is a class that implements an interface. An interface, such as an abstract class, defines how you can interact with an instance that implements that interface. You interact with an instance through member functions, so an interface typically declares virtual member functions that are meant to be overridden (implemented) by an implementing class (concrete class). If I have an abstract class Animal, it might have a virtual member function named speak. Animals all make different sounds, so the Animal interface does not know how to define that function. It will be up to the concrete classes, such as Dog, or Tiger, to define what actually happens when the speak function is invoked.

Could we declare a pointer of the class's type in the class declaration? [duplicate]

This question already has answers here:
What are the differences between struct and class in C++?
(30 answers)
Closed 8 years ago.
I know that is possible for a struct:
struct A{
A* p;
};
But could we do that to a class:
class B{
B*p;
}
I tried to google it, but end not found it. Thank you very much!
Yes. There is no difference between a class declared with struct and one declared with class other than the default access for members and bases.
Yes, a class can contain pointers to a class of the same type, and the symbol representing the class can appear in signatures of functions or in the bodies of functions defined in that class. Furthermore, it should be noted that, in C++, struct and class are identical with only one difference -- the default access level for a struct is "public", while the default access level for a class is "private" (of course, one can trivially write "public:" or "private:" at the beginning of either in order to change the access level; it is only convention that results in "class" being used for types that include more functionality, encapsulation and "struct" for pure data objects).

Nested Classes C++ [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicates:
nested classes C++
nested class access control in C++
I'm a little confused as to what access a nested class has to the members of an enclosing class. What is the relationship it shares with the outer class?
The nested class does not have any special access to the enclosing class that would not be available to any other class - it can see public members, etc. The nesting only provides a useful scoping mechanism for nested class, but doesn't change its behavior or capabilities.
This might help you : nested class access control in C++. It is not exacly what you asked, but gives some interesting informations.

C++ equivalent of "super"? [duplicate]

This question already has answers here:
Using "super" in C++
(19 answers)
Closed 9 years ago.
In Java, instead of using the scope operator, super is used ex:
C++ -> GenericBase::SomeVirtualFunction();
Java -> super.someVirtualMethod();
Is there something like this in C++ or does this not make sense in C++ because of multiple inheritance?
Thanks
There is a convention of defining a super typedef in every class.
There's no such thing in C++, although you can provide your own typedef :
struct Derived : Base
{
typedef Base super;
};
Microsofts compilers have (rejected by C++ standard commitee) extension __super.
Edit: Super may confuse readers of code. Because of multiple inheritance in C++ it is better to be more explicit. Multiple inheritance is already complex. There was AFAIK discussion about usefulness of super for templates that calmed down after it was realized that anyone can typedef super if they need it.
The typedef trick in Martin's link works quite well (and that's partial reason that's why C++ doesn't have super or inherited keywork AFAIR.)
The only thing we need to care about is that the typedef should be in private section. Don't put it in protected or public section, otherwise, an derived class may wrong use the typedef to refer to its grandparent rather than its parent.

What are real-world examples of C++ multiple inheritance? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Besides textbook examples -- in the real world -- does it ever make sense to use multiple inheritance (where more than one of the base classes are not pure interfaces) in C++?
It's very common when using Policy-based design, for example.
Easiest to point at iostream, which inherits from istream and ostream. cin and cout are istream and ostream respectively but fstream derives from iostream.
Microsoft's ATL (Active Template Library) uses multiple inheritance as mixin classes:
http://en.wikipedia.org/wiki/Active_Template_Library
IMO, it's open to argument. Prior to templates, there were a fair number of cases that justified it. Basically, you could use base classes about like you can use policy classes with templates. For example, consider a template like:
template <typename policy1, typename policy2>
class whatever {
policy1 p1;
policy2 p2;
public:
void dosomething() { if (p1.enquire()) p2.dosomething(); }
};
If you'd rather not use templates for some reason, you can get (some) fairly similar capabilities with multiple inheritance:
class whatever : policy1, policy2 {
void dosomething() { if (enquire()) dosomething(); }
};
In both cases, you're basically embedding an instance of the "policy" into your new object. Since we're using private inheritance, there's no concern with the LSP -- much like with policy-based templates, the intent isn't to create a large hierarchy that asserts anything about relationships between the members of the hierarchy -- rather, it's simply about creating the ability to create a large variety of unrelated classes on demand.
I have used multiple inheritance in the 'real world' - in a job where I was involved in coding a mapping application.
Unfortunately, I cannot remember the exact example which I find incredibly frustrating.
However, here is an interesting article about points to consider and alternative patterns or solutions to consider using:
http://www.parashift.com/c++-faq-lite/multiple-inheritance.html#faq-25.2
I like the use of vehicles for the examples - particularly adding in the amphibious vehicle.
Multiple inheritance definitely has its place and it can be very useful. As a general guideline a base class should be abstract whenever possible, which means you shouldnt be able to create an object out of it, but nothing stop you from inheriting from concrete class. It is definitely a merit too t hat you can take advantage of the inherited implementation from base class. Example on one of the answer on iostream is a good one. Another example is perhaps to say modelling a employee who is also an owner/director of a business and you will model it as
public class Director
{
................
double CalculateDividend();
bool ApproveBudget();
};
public class Employee
{
................
double CalculateSalary();
};
public class WorkingDirector: Employee, Director
{
..............
};
Now a WorkingDirector object can do what an Employee and a Director can do, which is a perfect in realworld. We wouldnt even need to overwrite any method implementation.
In many cases implementing design patterns are also made easier with multiple inheritance support.
If we have a class batsman which has data members like no of runs,no. of sixes, no of fours, batting average, run-rate etc. We have another class 'Bowler' which may have data members like no.of wickets taken, runs-per-over, average-wicket etc. For a player who is an all rounder, the class 'All-Rounder' will be derived from both the classes 'Batsman' and "Bowler'.
This can be cited as a real world example of multiple inheritance