9 Replies - 798 Views - Last Post: 19 January 2010 - 04:53 AM Rate Topic: -----

#1 Bl4ckC4t  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 17-January 10

C++ Inheritance question

Posted 18 January 2010 - 01:39 AM

Before anyone asks, this is not a homework assignment that I have, its a question of my own, as I am trying to get ahead in learning C++ as much as possible before my c++ course actually begins (in 6 months)

I know classes are a big part of C++ and help to make it more readable. However, I am unclear as to how classes really work.

I know this much:

class randomclass{
protected:
/* Protected code here */
private:
/* Private code here */
public:
/* Public code here */
};



My question on this part, what type of code is recommended in each part of the class? For instance, what type of code would go in the public class, protected class, and private class assignments?

I usually leave all code in the public, because I never know what would be best in each area.


Next question:

What is the difference between these two class inheritences: randomclass.variable vs randomclass->variable?

Thanks for the help in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: C++ Inheritance question

#2 ice-t  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 104
  • Joined: 26-May 09

Re: C++ Inheritance question

Posted 18 January 2010 - 01:51 AM

i could give u a hint on the public/private/protected.
for example
class A {
	   private:
		  // here u put the variables u use in ur class
			   int a;
			   string y;
	   public: 
		  // here u initialize the variables, u can  do that through a constructor
			 A(int b, string z) 
				   { 
						   a = b;
						   y=z;
					}
			void foo(){
								  cout << a << " " << y << endl;
						}
};


Protected u use when u use inheritance because if u make it private u cant access the variables.

U make an object of class A to access the method functions or variables
A ob(2,"test");
ob.foo(); // to access the method function

A *ob = new A(3,"test"); // or u can make it a pointer
ob->foo();



To access the variables u need another function getVariable() to return the variables from the private area or u can use the keyword friend.Making a method friend gives u access to the private part of the class

Hope i helped a bit, i too am a beginner in C++, cheers !

This post has been edited by ice-t: 18 January 2010 - 02:07 AM

Was This Post Helpful? 1
  • +
  • -

#3 Bl4ckC4t  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 17-January 10

Re: C++ Inheritance question

Posted 18 January 2010 - 01:53 AM

View Postice-t, on 18 Jan, 2010 - 12:51 AM, said:

i could give u a hint on the public/private/protected.
for example
class A {
	   private:
		  // here u put the variables u use in ur class
			   int a;
			   string y;
	   public: 
		  // here u initialize the variables, u can  do that through a constructor
			 A(int b, string z) 
				   { 
						   a = b;
						   y=z;
					}
			void foo();
};


Protected u use when u use inheritance because if u make it private u cant access the variables.

U make an object of class A to access the method functions or variables
A ob;
ob.foo(); // to access the method function

A *ob; // or u can make it a pointer
ob->foo();



To access the variables u need another function getVariable() to return the variables from the private area or u can use the keyword friend.Making a method friend gives u access to the private part of the class

Hope i helped a bit, i too am a beginner in C++, cheers !



This does help alot, actually. It was the first good explanation I have seen. Most of the C++ books don't really tell me all that much about classes, just how to program them -_-
Was This Post Helpful? 0
  • +
  • -

#4 ice-t  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 104
  • Joined: 26-May 09

Re: C++ Inheritance question

Posted 18 January 2010 - 02:12 AM

take another look , the first time i forgot i used a constructor and i didnt declare the object and the pointer the right way.

Cheers !

This post has been edited by ice-t: 18 January 2010 - 02:12 AM

Was This Post Helpful? 0
  • +
  • -

#5 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 855
  • View blog
  • Posts: 2,338
  • Joined: 20-August 07

Re: C++ Inheritance question

Posted 18 January 2010 - 03:27 AM

View PostBl4ckC4t, on 18 Jan, 2010 - 08:53 AM, said:

Most of the C++ books don't really tell me all that much about classes, just how to program them -_-

You could fill up several books talking about the finer points of OO design, but the short of classes from a plain language perspective its just a simple way to organise code, with the intention of building restrictions in the way that code can be used; i.e. make sure that the code you write can only be used in the way you've intended.

Using C++ as a procedural language, you'll often end up with code which looks like this
struct foo
{
    int a,b;
    string bar;
};

void func(foo& obj)
{
    cout << obj.a << obj.b << obj.bar;
}

foo initialise_foo(int a_, int b_, string bar_)
{
    foo temp;
    temp.a = a_;
    temp.b = b_;
    temp.bar = bar_;
    return foo;
} 
What the code does is unimportant, but there's nothing really stopping anyone abusing 'foo' and making a mistake which might resonate further down the line with your well formed functions. the way that the code is used shows that there's no relationship between foo and the functions which logically belong to it. e.g. It would be possible for someone to call func() on a foo object without having set any values for that foo object - you'd just get garbage output.
int main()
{
    foo object;
    object = initialise_foo(1,2,"hello");
    func(object);
} 



With an OO approach, those functions would become part of the class; initialise_foo, used to give some initial values to a foo object, would become a constructor - ensuring that the class can only be used alongside code which provides it with a default 'state' (i.e. its variables aren't simply left un-initialised after you create a foo object)
class foo
{
    int a, b;
    string bar;
public:
    foo(int a_, int b_, string bar_)
    {
        a = a_;
        b = b_;
        bar = bar_;
    }
    void func()
    {
        cout << a << b <<bar;
    }
}; 
On the face of it, there's only one syntactic difference in calling conventions, there is also no object prefix before the name of class variables - e.g. object.a is now just 'a' since the function belongs to the foo class, it always knows which foo object to work with.

Tthe advantage of organising data like this, is that you've suddenly built in a whole bunch of restrictions which prevent external bits of code (code unrelated to 'foo') from doing something bad which might result in bugs. e.g. with the constructor in place, the compiler will throw an error if you try to create an object without initialising it. main() now looks like this
int main()
{
    foo object(1,2,"hello");
    object.func();
} 
Some operations must be performed in a certain order - the first procedural program has various 'steps' which must occur in a particular order, but nothing about the foo class enforces that order. the second object-based program makes sure that the code using the foo class cannot perform those steps out of sequence; this is one of the main goals of object-based design. (OO design goes much deeper, but it builds on this idea)

This post has been edited by Bench: 18 January 2010 - 03:34 AM

Was This Post Helpful? 1
  • +
  • -

#6 sonicpp  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 58
  • Joined: 08-January 10

Re: C++ Inheritance question

Posted 18 January 2010 - 12:31 PM

There is a reason these three words are used... they mean exactly what they say...

The public keyword means anyone using your class can access that data/function.
The protected keyword means only classes that inherit your class can access that data/function.
The private keyword means just your class can access that data/function.

There is even more to all this... but for a quick explanation it should do.
Was This Post Helpful? 0
  • +
  • -

#7 Ratstail91  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 11-January 10

Re: C++ Inheritance question

Posted 18 January 2010 - 11:05 PM

I just thought I'd but in.
this is how i use the private/protected variables, it's weird, i know, but it works.

class Image {
	private:
		//
	protected:
		int x, y;
	public:
		Image(); //etc.
		~Image(); //etc.
		
		int X() { return x; }
		int Y() { return y; }
		void X(int a) { x = a; }
		void Y(int a) { y = a; }
};



I find that this is useful, god knows where i started this habit. enjoy!
Was This Post Helpful? 0
  • +
  • -

#8 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 855
  • View blog
  • Posts: 2,338
  • Joined: 20-August 07

Re: C++ Inheritance question

Posted 19 January 2010 - 03:13 AM

View PostRatstail91, on 19 Jan, 2010 - 06:05 AM, said:

class Image {
	private:
		//
	protected:
		int x, y;
	public:
		Image(); //etc.
		~Image(); //etc.
		
		int X() { return x; }
		int Y() { return y; }
		void X(int a) { x = a; }
		void Y(int a) { y = a; }
};



I find that this is useful, god knows where i started this habit. enjoy!
Well, now would be a good time to break that habit. You're leaking the implementation of your class to derived classes - this is a bad thing, because your class has no knowledge of its derived classes.
The core principle of the OO ideology is that implementation details must be hidden away, in order to restrict users of that class into a particular interface - by "users" that includes any code which that class has no knowledge of - obviously this includes derived classes. OO design rules exist to make life easier, if you break them, then programs which use your code may become harder to maintain.

a class' interface should ensure that your class can't be abused - i.e. that its impossible for some external bit of code which your class has no knowledge of to break that class through bypassing its interface. It should also be possible for implementation details to change without users of your class to know about it, e.g. if your class had been used in a dozen different places, then you decided that it'd be better to put X and Y into their own struct or class, this would be OK if the only changes were to get/set functions, but with protected data, you'd also need to modify the implementation of every single derived class too.
Was This Post Helpful? 0
  • +
  • -

#9 sou.bhowmick  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 19-January 10

Re: C++ Inheritance question

Posted 19 January 2010 - 04:45 AM

here are the answers for ur questio........

in the private section whatever member funtion or data members are declared those can be accessed only from the class itself.
i.e. u can use those members only within the class from any member functions those may be private,public or protected

in the public area whatever u declared can be accessed from anywhere inside or outside the class.
let us take an example..........
#include<iostream.h>
class A
{
	   private:
			int a,b;
			void display(){cout<<"this is private"<<endl;}
	   public:
			 void call(){display();}
};
void main()
{
	   A ob;
	   cout<<ob.a<<"	 "<<ob.b;//error
	   ob.display();//error
	   ob.call();//run correctly
}


the protected can now be easily understood by u while reading from some text book....................................
Was This Post Helpful? 0
  • +
  • -

#10 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 855
  • View blog
  • Posts: 2,338
  • Joined: 20-August 07

Re: C++ Inheritance question

Posted 19 January 2010 - 04:53 AM

View Postsou.bhowmick, on 19 Jan, 2010 - 11:45 AM, said:

 #include<iostream.h> 
This should be <iostream> - have a look here http://www.comeaucom...learn/faq/#q3.5


View Postsou.bhowmick, on 19 Jan, 2010 - 11:45 AM, said:

void main()
Read here to see why that's bad http://www.comeaucom...htalk/#voidmain

This post has been edited by Bench: 19 January 2010 - 04:55 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1