Hello
I'm new to object oriented programming. I read serval tutorial over the Internet but there is no clear explanation of my question.
Can anybody give me answer of following question related to object orientated programming ?
My questions are following :
1: what do you mean by object attribute ? how i can find or defined object attribute ?
suppose i have a class animal and its object like following
class animal{
{ int tits ;
string name;
void show()
{ cout<<"the animall has tits";
}
}
animal a;
Now can anyone tell me what will be the attribute of a?
2:What is object state ? Can any body explain with example the state of object?why we need the state of object ?
3: what is context of object ? what will be the context of a ?
4:How we gonna find whether a object is mutable or immutable ? How to make mutable object or immutable object ?
5: How does the factory object work ?
6: What is the meaning of iterating over some thing why we do this ?
Please give me answer of question with example code that will be very great help for me . please help this novice fellow.
ans 1: attributes of "a" are (1) int tits (2) string name
ans 2: The state of a object is the data that is required at any simulation time(for example) to determine the object's actions at the next simulation time, given an event to process. Normally, the only data that needs to be in an object's state is data that can change each simulation cycle.
ans 4: If the state of object can be changed it is immutable
ans 5: factory object is an object decided by subclasses which class to instantiate. The Factory method lets a class defer instantiation to subclasses.
ans 6: iteraion means looping. We use it prevent us from re-writing a block of code which is to be executed repeatidly.
e.g.,
1 2 3 4
for (i = 0; i < 10; i++)
{
cout << "My name is snakec....Yahoooooo!\n";
}
it will print the line "My name is snakec....Yahoooooo!" 10 times on screnn
Hi
Thank You for helping me.
I understand the concept of object attribute but still confused by object state can you elaborate it for me or explain it properly . i didn't understand the concept of simulation time what is simulation time why we need it.
its a bit silly question but can u tell me what is instance , or instance variable of a class , are they differ from object of class .I'm quite confused between instance and object.
if a class has no member variables and no static variables and there are no truly global variables, there is no way it can hold state - every method would be just like a mathematical function where the result only depends on the arguments passed in and on nothing else
member variables are variables specific to an instance only - an instance has its own set of member variables that take memory space and has actual values
that means when you call a method on an instance that has member variables, there is a possibility that the result will depend on those member variables - it can depend on the current state of the instance
definitions:
class - is like a template for an instance: it requires no memory (it's just a blueprint)
instance - is a specific instantiation of a class that actual uses up memory
object - often used as a synonym for instance above
instance variable or member variable - is like a field in a struct: for every instance/object that you create, it has this member variable inside (eg in an User object, name, address, and phone number may be fields or instance variables)
now, an instance variable may be POD or plain-old-data like double or char, but it may also be an instance of another class type like std::string
so an instance or object takes memory whereas a class does not
an instance variable is just one of the fields inside your instance
ans 4: If the state of object can be changed it is immutable
You mean "it is mutable"
@snakec:
Objects and classes are all about abstraction. Think about an real-world object, like a car. A car has many things that make up what it is, things that describe it (adjectives if you will) also know as its state. Like: It's speed, acceleration, direction, size, mass, gas mileage, etc... It also has things it can do (verbs) also known as behavior. Like: It can drive, brake, change gears, start up, etc... In programing an object's state(s) are described by its attributes or fields (think variables.) An object's behavior is described by its methods or member functions (anything non-static in a class is called a member, excluding the constructors.)
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13
class Car
{
//attributes
double speed;
double direction;
constdouble gasMileage;
//etc...
//methods
void brake();
void drive(double distance);
//etc...
};
Objects whose state will never change are know as immutable objects. Let's say I define a 2D point as such:
1 2 3 4 5 6 7 8 9 10 11 12 13
struct Point
{
constdouble x, y;
Point() : x(0), y(0) {}
Point(double _x, double _y) : x(_x), y(_y) {}
Point(Point &p) x(p.x), y(p.y) {}
Point operator+(const Point &p) const { return Point(x + p.x, y + p.y); }
Point operator-() const { return Point(-x, -y); }
Point operator-(const Point &p) const { return *this + -p; }
//...
};
Point objects defined as above are now immutable. In C++ the idea of an immutable object is a bit lost. As you can see above, in order for me to make the Point class immutable I had to leave out the definition for its assignment operator(s). Most objects are defined as immutable in C++ by using the const keyword. The only time this can be a bit shady is when the members of a class include pointers or references (not illustrated above.) In which case immutable classes will not allow non-const methods to invoke a pointer's or reference's non-const methods/operations.