PCC - CS246 - OOP FINAL - Dr. Howell
What is the difference between () and [] when related to arrays of objects?
() defines an object passing a certain value to the constructor. [] allocates and array with a certain size, which is uninitialized.
What are the 3 different ways for initializing an object?
*unsure *3 ways for copy constr. myclass ob1; <-- Initializing at definition. myclass ob2(1); <-- myclass ob3(myclass(7)); <-- //************************* 1). myclass x = y; 2). func(y); 3). y = func( ); // PAGE 367 //*************************
What is the difference between . -> & :: * when pertaining to pointer? What are there names?
. (dot operator) between object names/members: ob1.iv -> (arrow operator) between pointers/members: p_ob1.iv & (reference operator) in proto. and header func(myclass &ob1) :: (scope resolution operator) myclass::func(); * (pointer operator) in proto. and header func(myclass *ob1) in call func(&ob1) <--need to send address
What are the steps to returning an object from a function?
1. 1. Create a temporary object. | 2. If memory is involved: copy constr. allocates memory 2. 3. Move the original's value to the copy | 4. Show an allocation error message (maybe). 3. 5. Destruct the original. 4. 6. The copied object replaces the func. call and is used.
Why do we use inline functions?
1. More efficient code 2.Faster run-time
How do new and delete differ from malloc and free?
1. New allocates enough memory for the object 2. Automatically creates a return type 3. New and delete can be overloaded 4. New and delete are operators 5. You can initialize allocated memory
How do we overload functions?
1. Number of parameters 2.Type of parameters
What are the advantages of using a reference?
1. Programmer does not have to pass address 2. Many references are clearer and more elegant code 3. No copy of the object is made 4. Faster than call by value
5 Methodologies of Programming. (In order)
1. Toggle Switches 2. Assembly Languages 3. High Level Languages 4. Structured Programming 5. Object Oriented
What are the 3 initialization conditions in which copy constructors are called?
1. When one object explicitly initializes another. Ex myclass x=y; 2. When a copy of an object is made to be passed to a function. Ex func(y); 3. When a temp object is generated Ex y = func();
Why do you overload functions?
1.Flexibility 2.Create initialized and uninitialized objects 3.To define copy constructors
When did he invent it?
1979.
What does the THIS pointer point to?
A pointer to the object that invoking objects
What is the THIS pointer for?
A pointer which points to the invoking object.
What gets copied when you use an assignment operator on an object?
An exact bitwise copy is made (Unless the assignment operator is overloaded).
What is the name of a one-dimensional array of objects?
An object array **
Why do we not use windows to write programs?
Because there is too much overhead to use windows.h, and other windows processes.
Where did he invent it?
Bell Laboratories, Murray Hills, NJ.
Encapsulation
Binds together code and data and keeps it safe. No private members can be access by code outside the class. Typically code is public but data is private.
Who invented C++?
Bjarne Stroustrup
How can a derived class access private/public/protected member?
Box in Notes
What level of language is C++?
C++ is a medium level language.
How do you return an object?
Carefully. return ob1;
What is the class keyword?
Class
Can constructors and destructors take default parameters?
Constructors can.
Can constructors and destructors take more than one parameter?
Constructors can.
How do we name constructors and destructors?
Constructors: Same as the name of the class Destructor: ~Same as the name of the class
What are the 3 ways to solve the problem of an object trying to point to memory that does not exist?
Copy constructor Pass by pointer Pass by reference
What is the purpose of the destructors?
Dellocates an allocated memory of the class.
When and how is encapsulation used.
Encapsulation is used when a class is defined above main, having private variables and public functions.
T/F Is C++ a subset of C?
False. C is a subset of C++.
Who is the author of the book?
Herbert Schildt
Where can the short and long method of initializing array be used?
If there are 2 or more arguments, need to use the long form.
What is the difference between inline and auto-inline functions?
Inlined: class myclass { int iv; public: void func(int a); } inline void myclass::func(int a) {...} ______________________ Auto-inlined (what we did): class myclass { int iv; public: void func(int a) {...} }
What is the difference between Built-In Variables and Programming Defined Variables.
Int is a built in variable type. Struct mystruct is a user-defined type.
What is the purpose of friend functions?
It has access to all private and protected members.
How do we indicate that a copy constructor is a copy constructor?
Key word CONST
What are the disadvantages of using a reference?
Looks exactly the same as call-by-value. Cant tell the difference
Polymorphism
Many Forms , One Name
What is the difference between member variables and member functions.
Member variable : You want them as private Member Functions : You want them as public
Member Variable/Member Functions Which ones are typically private and which are typically public?
Member variables are private (to keep them safe). Member functions are public (to access variables in a safe way).
Which functions can be friends?
Member, Non-member
Can we overload a function if they differ only in their return type?
NO!!
Is C++ the only OOL language?
NO.
Can destructors be overloaded?
No
Can friend functions be overloaded?
No
Can you access and array of objects with references?
No
Are friend functions inherited?
No they are not
Can operator overloading change operator precedence?
No, it does not.
When you define a class, using the class keyword, is memory allocated?
No, the definition above main tells the computer what the class is. myclass ob1 statically allocates memory at compile time. new myclass dynamically allocates memory at run time.
Can you assign two objects to each other from different classes? (Even if the classes are basically identical, except for the name).
No, the object has to be the same type. (pg 324)
Do classes require constructors and destructors?
No, they do not.
Are overloaded operator functions inherited?
No.
When you overload a function, is the keyword OVERLOAD required?
Not anymore.
What does OO stand for?
Object Orientation
What does OOP stand for?
Object-Oriented Programming
What does OOPL stand for?
Object-Oriented Programming Language
What does using the word VIRTUAL prevent?
Prevents duplication of parent object. Solves the problem of having to copies of the base class.
What does one mean by private/public/protected?
Private: Only accessible by members. Public: Accessible by the derived class Protected: A protected member is not accessible by other non-member elements of the program.
How can friend function help successfully overload operators?
Problem: 1 + ob; ob + 1; Sollusion: class friend operator+(int a, class
Based / Derived Classes Which inherits which?
The Derived Classes inherit the properties of the Base Class
How do inline functions work?
The compiler replaces the call with the code.
What is the constructor and destructor return type?
They do not have return types.
Why do we use new and delete?
To dynamically allocate memory To release the dynamically allocated memory back to the system
Why would you want to overload an operator?
To give an operator an additional purpose
What is the purpose of constructors?
To initialize the member variables.
Why create a copy constructor?
To initialize, not assign
How do we make a friend function?
Use the keyword FRIEND
Where do we define objects in our programs?
Usually top of function (main) code, but rarely one may define an object after an important value has been obtained. main { cin >> value; myclass ob1(value);
Can you return an object from a function by value/reference/pointer?
Value - Yes Reference - Yes Pointer - Yes
Can you call a function and pass an object by value/reference/pointer?
Value: Yes Reference: Yes Pointer: Yes
What happens to n++, if n was passed by value/reference/pointer?
Value: changes n in function. Reference: changes original n. Pointer: changes original n.
How is encapsulation violated?
When member variables are accessed without using member functions.
What ambiguities can come from overloading?
When the compiler cannot decide which functions to use because of a bad call.
Can constructors be overloaded?
Yes
Can one functions be a friend to two or more classes?
Yes
Can you access and array of objects with indexes?
Yes
Can you access and array of objects with pointers?
Yes
Can you set up two and three-dimensional arrays of objects?
Yes
Can you have multiple inheritance and in what ways can that be accomplished?
Yes b ' ' ' ' ' ' b ' ' ' ' ' ' ' ' 'b | ' ' ' ' ' '/ \ ' ' ' ' ' ' ' ' '| d1 ' ' ' ' d1 d2 ' ' ' ' ' d1 | ' ' ' ' ' '\ / ' ' ' ' ' ' / | \ d2 ' ' ' ' 'd3 ' ' ' ' d2 d3 d4 ..........^^^this one needs virtual to prevent multiple bases
Is a copy constructor a constructor that is overloaded?
Yes it is
Can friend functions have parameters?
Yes they can
Can a function appear on the left side of an operator?
Yes.
In C++, are you forced to prototype a function?
Yes.
How does one use the C libraries in C++?
You do not use the .h with the includes.
How does one use string.h in C++?
You use #include <cstring>
Which operators cannot be overloaded?
[ . ] , [ :: ] , [ .* ] , [ ? ]
How does a compiler react to a non-defined class?
^%&$%#*$!!! ERROR!!! It wants a forward reference: class c2; <--forward reference c1{ int a; public: void func(c2 ob); <--If no forward reference; the } computer would scream here. c2{ int b; }
Whats do cin and cout do?
cin takes input (>>), and cout (<<) outputs to the screen.
How do you initialize static array the short way?
cl ob[3] = {1,2,3};
How do you initialize static array the long way?
cl ob[3] = {cl(1), cl(2), cl(3)};
How do you set up an array of object?
cl ob[3];
How do you initialize a two dimensional array?
cl ob[] = {
When are constructors and destructors executed?
constructor is executed when the objects declaration statement is encountered. The destructor is called in reverse order.
Can constructors and destructors take parameters?
constructors can, but destructors cannot.
Pointers to objects, define and use them.
myclass ob1, p_ob; p_ob = &ob1;
What is the definition of an object?
myclass ob1; <--Makes an object.
How do you allocate new dynamic memory, and how do you return it?
try catch new return a pointer to it
When are constructors called?
when the object comes into existence
When are destructors called?
when the object is destroyed