GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 27-Oct-2005, 12:32
jake_jeckel's Avatar
jake_jeckel jake_jeckel is offline
New Member
 
Join Date: Oct 2005
Posts: 9
jake_jeckel is on a distinguished road

Object Oriented Programming?


I missed my class about this object oriented programming...I read over the notes but he uses statements that I don't really understand.

Can anyone explain what object oriented programming is in just a few lines?
(or translate this into...lamens terms i suppose)

This is what my prof said...
"Decomposition of the problem domain into a set of collaborating abstract data types (i.e., classes), each of which encapsulates mutable states that can only be accessed via a well-defined ADT interface.

It is often argued that the paradigm of object orientation results in abstractions that map naturally to concepts in the problem domain, and supports graceful evolution of software through encapsulation. "



And another question...looking through his example of OOP, he has this array.
Okay, he's got this "Guarded Array" and this "Managed Array". And the code is as follows:
CPP / C++ / C Code:
// GuardedArray
// - A wrapper class for C++ arrays to make array access safe.
//   Specifically, initialization is guaranteed, and assertions are
//   in place to detect array index out of bound errors in array member
//   accesses.
//

class GuardedArray {
public:
  //
  // Constructor
  //
  // Purpose: Initializes array elements to zeros
  // Argument(s): N/A
  // Side Effect: All array elements are initialized to zero.
  // Return: N/A
  //

  GuardedArray();

  //
  // Constructor
  //
  // Purpose: Initializes all array elements to a given value.
  // Argument(s):
  //  x : value to which array elements should be initialized.
  // Side Effect: All array elements are initialized to x.
  // Return: N/A
  //

  GuardedArray(ItemType x);
And the actual implementation of the code
CPP / C++ / C Code:
GuardedArray::GuardedArray() {
  for (unsigned i = 0; i < MAX_LENGTH; i++)
    array[i] = 0;
}

GuardedArray::GuardedArray(ItemType x) {
  for (unsigned i = 0; i < MAX_LENGTH; i++)
    array[i] = x;

And he made this new type of array which uses the guarded array module

CPP / C++ / C Code:
// ManagedArray
// - A wrapper class for C++ arrays to facilitate insertion and removal of
//   array elements.
// - Every instance of ManagedArray has a size that can be increased
//   until the maximum capacity MAX_LENGTH is reached.
//

class ManagedArray {
public:
  //
  // Constructor
  //
  // Purpose: Initializes array to have zero size.
  // Argument(s): N/A
  // Side Effect: The array is initialized to be empty.
  // Return: N/A
  //

  ManagedArray();

  //
  // Constructor
  //
  // Purpose: Initializes array to a given size.  All array elements
  //          are initialized to zero.
  // Argument(s):
  //  N : size of array
  // Precondition: N <= MAX_LENGTH
  // Side Effect: The array is initialized to contain N occurrences
  //              of the number zero.
  // Return: N/A
  //

  ManagedArray(unsigned N);

  //
  // Constructor
  //
  // Purpose: Initializes array to a given size.  All array elements
  //          are initialized to x.
  // Argument(s):
  //  N : size of array
  //  x : initial value of array elements
  // Precondition: N <= MAX_LENGTH
  // Side Effect: The array is initialized to contain N occurrences
  //              of x.
  // Return: N/A
  //

  ManagedArray(unsigned N, ItemType x);

  // more function prototypes are there but i'm not confused about them

and the implementation...
CPP / C++ / C Code:
ManagedArray::ManagedArray() : array() {
  count = 0;
}

ManagedArray::ManagedArray(unsigned N) : array() {
  assert(N <= MAX_LENGTH);
  count = N;
}

ManagedArray::ManagedArray(unsigned N, ItemType x) : array(x) {
  assert(N <= MAX_LENGTH);
  count = N;
}

The thing i'm confused about is the " : array() " in the code
ManagedArray::ManagedArray(unsigned N) : array()
(which is clearly in the code just above)

What does that do and how does this initialize the array?

I think he tried explaining it by
"When a constructor is invoked, C++ semantics dictate that the constructors of member fields are called prior to the beginning of the constructor body. Usually, the default constructors of the member fields are invoked in such cases. Programmers may override this behavior, and explicitly select the constructors to be invoked on the member fields by using an explicit member initializer ": field(...)". "

But I can't even begin to understand what that means...any help is very much appreciated.

Jake
  #2  
Old 27-Oct-2005, 15:59
jake_jeckel's Avatar
jake_jeckel jake_jeckel is offline
New Member
 
Join Date: Oct 2005
Posts: 9
jake_jeckel is on a distinguished road

Re: Object Oriented Programming?


i forgot to include that array () is declared in the private section of the GuardedArray class (first code i pasted)
and in the private section of the ManagedArray class has "GuardedArray array;"
  #3  
Old 28-Oct-2005, 00:02
Gamer_2k4's Avatar
Gamer_2k4 Gamer_2k4 is offline
Member
 
Join Date: Apr 2005
Location: Wisconsin
Posts: 117
Gamer_2k4 will become famous soon enough

Re: Object Oriented Programming?


I have no idea why your instructor is making this so complicated...Object-oriented programming should be fairly simple. C++ uses a data structure called a class, which contains functions and variables. In addition, those functions and variables may be declared public (they can be accessed anywhere in your program) or private (they may only be accessed by the class they belong to). A class is a sort of data type, like an integer or a boolean. Instances of classes are called objects. Classes also must have constructors: functions in that class which initialize the variables. Here is an example class:

CPP / C++ / C Code:
class Cat              //start of class declaration
{
public:      //the following functions and variables may be accessed anywhere 
  Cat();               //basic constructor
  Cat(int age);      //overloaded constructor
  void Speak();     //a function in this class
  void setAge(int age);
  int getAge();     

private:   //the following functions and variables may be accessed only from
             //objects of this class 
  int myAge;
};                      //end of class declaration

//defining the Cat functions
Cat::Cat()          //class::function(arguments) 
{
  myAge = 0;
}

Cat::Cat(int age) //overloading the constructor to set the cat's age
{
  myAge = age;
}

void Cat::Speak()
{
  std::cout << "Meow.\n";  //an example function of the class
}

Cat::setAge(int age)
{
  myAge = age;
}


int Cat::getAge()
{
  return myAge;           //an accessor function, so other parts of the program
}                              //can get the age

Now this is how we would use the above code in our program:
CPP / C++ / C Code:
#include <iostream>
#include <stdlib.h>

int main()
{
  Cat Fluffy();
  std::cout << "Fluffy is " << Fluffy.getAge() << " years old.\n";
  Fluffy.setAge(5);
  std::cout << "Fluffy is now " << Fluffy.getAge() << " years old.\n";
  Fluffy.Speak();

  Cat Bob(3);  //ok, maybe Bob isn't exactly a cat's name.  oh, well...
  std::cout << "Bob is " << Bob.getAge() << " years old.";
  Bob.setAge(6);
  std::cout << "Bob is now " << Bob.getAge() << " years old.";
  Bob.Speak();
}

The output should look like this:

0
5
Meow.
3
6
Meow.

I hope that sort of helps you understand object oriented programming. If you need any more help, my email address is gamer_2k4@yahoo.com.
  #4  
Old 28-Oct-2005, 00:17
kobi_hikri's Avatar
kobi_hikri kobi_hikri is offline
Regular Member
 
Join Date: Apr 2005
Location: Israel
Posts: 431
kobi_hikri has a spectacular aura aboutkobi_hikri has a spectacular aura about

Re: Object Oriented Programming?


Quote:
Originally Posted by Gamer_2k4
I have no idea why your instructor is making this so complicated...

His proffesor is doing a good job. His explaining the why as well as the how.
Many times, the why is much more important.
If you understand a subject, you can take it along to the next programming language.

To the OP : Here is an instructive link : Object-oriented programming in C
Kobi.
__________________
It's actually a one time thing (it just happens alot).
  #5  
Old 28-Oct-2005, 00:18
jake_jeckel's Avatar
jake_jeckel jake_jeckel is offline
New Member
 
Join Date: Oct 2005
Posts: 9
jake_jeckel is on a distinguished road

Re: Object Oriented Programming?


okay well forget about the object oriented aspect of this thread...i kinda understand it now so it's fine

But I still really would like to know the last part of my first post...

The thing i'm confused about is the " : array() " in the code
ManagedArray::ManagedArray(unsigned N) : array()
(which is clearly in the code just above)

What does that do and how does this initialize the array?

I think he tried explaining it by
"When a constructor is invoked, C++ semantics dictate that the constructors of member fields are called prior to the beginning of the constructor body. Usually, the default constructors of the member fields are invoked in such cases. Programmers may override this behavior, and explicitly select the constructors to be invoked on the member fields by using an explicit member initializer ": field(...)". "

If anyone could explain this...THANKS lol
  #6  
Old 28-Oct-2005, 01:50
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 929
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: Object Oriented Programming?


Hi Jack,

Here is some information:
Quote:
Originally Posted by Jack
The thing i'm confused about is the " : array() " in the code
ManagedArray::ManagedArray(unsigned N) : array()
(which is clearly in the code just above)

What does that do and how does this initialize the array?

I found this from a site:

It's a standard C++ way of initializing a class member. For
simplicity:

CPP / C++ / C Code:
class A { 
private: 
        int m_value; 
public: 
        A(); 


}; 


A::A() : m_value(0) 
{ 


} 

would have the same net result than:

CPP / C++ / C Code:
A::A() 
{ 
        m_value = 0; 


} 

However, if you change the class to:

CPP / C++ / C Code:
class A { 
private: 
        const int m_value; 
public: 
        A(); 


}; 

then trying to do:

CPP / C++ / C Code:
A::A() 
{ 
        m_value = 0; 


} 

would result to an error, since m_value is now constant and cannot be
modified at runtime. The only way to give this const value a value is:

CPP / C++ / C Code:
A::A() : m_value(0) 
{ 


} 

You can also use base-class constructor and initialize more variables,
for example:

CPP / C++ / C Code:
class A { 
private: 
        int m_a; 
public: 
        A(int a); 


}; 


A::A(int a) 
        : m_a(a) 
{ 


} 


class B : public A { 
private: 
        int m_b; 
        int m_c; 
public: 
        B(int b); 


}; 


B::B(int b) 
        : A(b), m_b(b+1), m_c(b+2) 
{ 


}

What you're doing is initializing the members or parent class using a constructor notation.
For example, if you had a class like this:
CPP / C++ / C Code:
class Foo 
{ 
    Foo(int X, int Y, int Z); 


}; 

You could then initialize it in a derived class using the constructor
notation:

CPP / C++ / C Code:
class Bar : public Foo 
{ 
    Bar() : Foo(2, 6, 2) {}; 


}; 

For simple data types such as int, it's basically the same as an assignment
statement, but the constructor notation really shines when you're dealing
with objects, and especially with inheritance.


Regards,
Paramesh.
__________________

Don't walk in front of me, I may not follow.
Don't walk behind me, I may not lead.
Just walk beside me and be my friend.
  #7  
Old 29-Oct-2005, 18:04
crystalattice's Avatar
crystalattice crystalattice is offline
Aspiring author
 
Join Date: Apr 2004
Location: Japan (again)
Posts: 1,628
crystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nice

Re: Object Oriented Programming?


Quote:
Originally Posted by kobi_hikri
His proffesor is doing a good job. His explaining the why as well as the how.
Many times, the why is much more important.
If you understand a subject, you can take it along to the next programming language.

To the OP : Here is an instructive link : Object-oriented programming in C
Kobi.
I think professor is right on with stating the why. It's just the fact that he uses big words to make his point. He could make the same point in a far simpler fashion.

For example,
Quote:
"Decomposition of the problem domain into a set of collaborating abstract data types (i.e., classes), each of which encapsulates mutable states that can only be accessed via a well-defined ADT interface.

It is often argued that the paradigm of object orientation results in abstractions that map naturally to concepts in the problem domain, and supports graceful evolution of software through encapsulation. "
An easier way of saying this could be: "When a problem is chopped down into to several component parts, each part can then be 'visualized' as an abstract data type known as a class. Each class has features that can only be accessed via a standard interface. Object-oriented programming makes abstraction of a problem easier by allowing a class to 'surround', or encapsulate, more specific functions. By dealing only with the 'outer wrapper' of a class and not the inner details, the final solution is easier to find. Plus classes are easier to reuse."

Just a thought.
__________________
Start Programming with Python-A beginner's guide to programming and the Python language.
-------------
Common Sense v2.0-Striving to make the world a little bit smarter.
  #8  
Old 30-Oct-2005, 03:36
kobi_hikri's Avatar
kobi_hikri kobi_hikri is offline
Regular Member
 
Join Date: Apr 2005
Location: Israel
Posts: 431
kobi_hikri has a spectacular aura aboutkobi_hikri has a spectacular aura about

Re: Object Oriented Programming?


Quote:
Originally Posted by crystalattice
It's just the fact that he uses big words to make his point. He could make the same point in a far simpler fashion.

We don't agree here.
The original poster will some day encounter thechnical books and there will be no one to make the point simple for him. Maybe his boss will prefer "big words", perhaps his cutomers or clients ...
It is important that students encounter problems such as this one while at university.

Kobi.
__________________
It's actually a one time thing (it just happens alot).
  #9  
Old 30-Oct-2005, 13:25
svenveer svenveer is offline
New Member
 
Join Date: Oct 2005
Posts: 15
svenveer is on a distinguished road

Re: Object Oriented Programming?


Jake
Quote:
"When a constructor is invoked, C++ semantics dictate that the constructors of member fields are called prior to the beginning of the constructor body. Usually, the default constructors of the member fields are invoked in such cases. Programmers may override this behavior, and explicitly select the constructors to be invoked on the member fields by using an explicit member initializer ": field(...)". "

But I can't even begin to understand what that means...any help is very much appreciated.

Jake

Actually, it's called a constructor initializer list. The initializer list allows you to initialize members and superclasses before the code of the constuctor is executed.

An example:

Code:
class foo{ int x; public: foo(int value) : x(value){} };

calling new foo(10) will initialize x with the value 10. Since int is not a class, this is called a pseudo constructor.

Code:
class bar{ string s; public: bar(string value) : s(value){} };

calling new bar("hiya") will initialize s with "hiya".

Code:
class foobar : public foo, public bar{ char *cp; public: foobar(int a, string b, char* c) : foo(a), bar(b), cp(c) { // some really useful code here } };

in this las example (multiple inheritance) we make sure that before the constructor code is executed both super classes are initialized and that our private member is initialized.
 
 

Recent GIDBlogToyota - 2009 May Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Read/ Write EXCEL files using C/C++ programming confused_pig C++ Forum 4 25-Nov-2005 01:27
[Tutorial] GUI programming with FLTK dsmith FLTK Forum 10 03-Oct-2005 16:41
GUI programming crystalattice C++ Forum 5 14-Sep-2004 13:17
gxx linker accepts only 7 object files danielxs66 C++ Forum 1 12-Dec-2003 10:27
How do I access an object from inside another object? JdS MySQL / PHP Forum 5 10-Jun-2003 09:51

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 15:57.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.