0% found this document useful (0 votes)
39 views4 pages

Question: What Do You Mean by Redirection ?:: OOP: Copy Constructors

A copy constructor is called when a new variable is initialized from an existing object, such as when declaring a variable initialized with another object, passing an object as a value parameter to a function, or returning an object from a function. If no copy constructor is defined, the compiler uses a default copy constructor that performs a shallow copy. A friend function can access the private and protected members of a class. It allows non-member functions and other classes to access the private data of a class by declaring them as friends. Overloading comparison operators like == and != for a class allows objects of that class to be compared. The overloaded operators are defined as friend functions that compare the private data members of the class objects.

Uploaded by

Anu Ji
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views4 pages

Question: What Do You Mean by Redirection ?:: OOP: Copy Constructors

A copy constructor is called when a new variable is initialized from an existing object, such as when declaring a variable initialized with another object, passing an object as a value parameter to a function, or returning an object from a function. If no copy constructor is defined, the compiler uses a default copy constructor that performs a shallow copy. A friend function can access the private and protected members of a class. It allows non-member functions and other classes to access the private data of a class by declaring them as friends. Overloading comparison operators like == and != for a class allows objects of that class to be compared. The overloaded operators are defined as friend functions that compare the private data members of the class objects.

Uploaded by

Anu Ji
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Question: What do you mean by redirection ?

Answer
With the help of redirection we can change the input output locations of the process ?

C++ Notes: OOP: Copy Constructors

When copies of objects are made


A copy constructor is called whenever a new variable is created from an object. This happens in
the following cases (but not in assignment).

 A variable is declared which is initialized from another object, eg,


 Person q("Mickey"); // constructor is used to build q.
 Person r(p); // copy constructor is used to build r.
 Person p = q; // copy constructor is used to initialize in
declaration.
p = q; // Assignment operator, no constructor or copy
constructor.

 A value parameter is initialized from its corresponding argument.

f(p); // copy constructor initializes formal value


parameter.

 An object is returned by a function.

C++ calls a copy constructor to make a copy of an object in each of the above cases. If there is
no copy constructor defined for the class, C++ uses the default copy constructor which copies
each field, ie, makes a shallow copy.

What is a Friend Function?

A friend function is used for accessing the non-public members of a class. A class can allow non-
member functions and other classes to access its own private data, by making them friends. Thus,
a friend function is an ordinary function or a member of another class
They're related but not totally comparable.

A pointer is an address in memory where a variable is located.

An array is a conceptual data representation consisting of a list of more than one item of a
particular scalar type (int, float, char, structure, etc.) where each element is accessed by its index.
The index can be thought of as a counter or enumerator telling you how many elements you have
to skip over to get to the one you're interested in. Here's where addresses come in ... the location
of a particular element in memory is offset from the so-called base address (i.e. the address of
the starting element) by the value

(sizeof(one element) * index #)

The C compiler is smart enough to take the sizeof() into account when you do pointer arithmetic,
so you can find the address of the i-th element as (address of base) + i, rather than having to do
the multiplication explicitly.

The idea of element address as offset also accounts for C's use of zero-relative array definitions.
Since the first element is offset by 0 positions from the first element its index in C is 0, not 1.
The second (ordinal) element is offset from the first by 1 position so its index is 1, and so on.

Read more:
http://wiki.answers.com/Q/Difference_between_pointers_and_arrays_in_C#ixzz1G1RqCfE2

9.4 — Overloading the comparison operators


By Alex, on October 4th, 2007

Overloading the comparison operators is simple once you’ve learned how to overload the
arithmetic operators.

Because the comparison operators are all binary operators that do not modify their operands, we
will make our overloaded comparison operators friend functions.

Here’s an example Point class from the previous lesson with an overloaded operator== and
operator!=.

view source

print?

01 class Point

02 {
03 private:

04     double m_dX, m_dY, m_dZ;

05  

06 public:

07     Point(double dX=0.0, double dY=0.0, double dZ=0.0)

08     {

09     m_dX = dX;

10     m_dY = dY;

11     m_dZ = dZ;

12     }

13  

14     friend bool operator== (Point &cP1, Point &cP2);

15     friend bool operator!= (Point &cP1, Point &cP2);

16  

17     double GetX() { return m_dX; }

18     double GetY() { return m_dY; }

19     double GetZ() { return m_dZ; }

20 };

21  

22 bool operator== (Point &cP1, Point &cP2)

23 {

24     return (cP1.m_dX == cP2.m_dX &&

25             cP1.m_dY == cP2.m_dY &&

26             cP1.m_dZ == cP2.m_dZ);

27 }

28  

29 bool operator!= (Point &cP1, Point &cP2)


30 {

31     return !(cP1 == cP2);

32 }

You might also like