0% found this document useful (0 votes)
35 views3 pages

Ifstream Infile : General Tips

This document provides a summary of key concepts in C++ including: 1) General tips for C++ syntax like class declarations requiring semicolons and constructor/method definition formats. 2) Pointer concepts such as const pointers, passing pointers to functions, dynamically allocating memory, and pointers to class objects/structures. 3) Copy constructors, memberwise assignment, overloaded assignment operators, and how to define copy constructors for classes with pointer members. 4) Overloading stream insertion/extraction operators and arithmetic operators. 5) Linked list concepts like using reference parameters to modify list elements and correctly assigning new list nodes.

Uploaded by

Daniel Watkins
Copyright
© © All Rights Reserved
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)
35 views3 pages

Ifstream Infile : General Tips

This document provides a summary of key concepts in C++ including: 1) General tips for C++ syntax like class declarations requiring semicolons and constructor/method definition formats. 2) Pointer concepts such as const pointers, passing pointers to functions, dynamically allocating memory, and pointers to class objects/structures. 3) Copy constructors, memberwise assignment, overloaded assignment operators, and how to define copy constructors for classes with pointer members. 4) Overloading stream insertion/extraction operators and arithmetic operators. 5) Linked list concepts like using reference parameters to modify list elements and correctly assigning new list nodes.

Uploaded by

Daniel Watkins
Copyright
© © All Rights Reserved
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/ 3

CS review

General tips:
- Class declarations require semicolon at the end
Class cat {

};
- General constructor define format is <object>::<object> (<object> &obj)
General method define format is <return type> <object>::<method name> (parameters)
- Condition ? X : Y
o If condition is true, return X; else, return Y
- If you make a copy constructor, just make a destructor “ ~<object>(); “
o If you do this, must define destructor too
- //first and second are reference parameters because they have to be modified
void Split(ListNode* head, ListNode* &first, ListNode* &second) {…}
- Ifstream inFile;
inFile.open(inputFileName.c_str());

Pointers:
- const int * ptr; //ptr points to a const int
- int * const ptr; //ptr itself is const, not the object it points to
o so, ptr = 0; //illegal
o but, *ptr = 0; //legal
- void setToZero (int* ptr) { …}
o int value;
setToZero(&value) //must pass in address
- ***Dynamic memory allocation is possible only with pointers
o Uses the ‘new’ operator
Pointers to int, double, etc.
o Int *ptr = new int;
 Once you allocate, must delete the allocated memory to free for future use
o Delete ptr;
ptr = 0;
o A function can only return a pointer of dynamically allocated storage (so the content in
the pointer should have been dynamically allocated). It CANNOT return a pointer to
local variables of the function since those variables are deallocated upon completion of
the function.
Pointers to class objects and structures
o Use “->” to dereference a pointer to a class object.
o When a pointer is a member of a class object (like a private member),
so like
struct GradeInfo {
String name;
Int *testScores;
}
GradeInfo student;
Cout << *student.testScores; //must add the * at the beginning
//or do this
GradeInfo* ptr;
Cout << *ptr->testScores //again, must add * at the beginning
o Pointer can dereference only a class object, not a member of a class object (member
requires the extra * at the beginning. -> dereferences ptr, *dereferences ptr-
>testScores)
Copy constructor/ memberwise assignment
- Memberwise assignment
o = operator can be used for objects too
o MoveOrder1 = MoveOrder2
//creates MoveOrder1 with the exact same member values as MoveOrder2
- Default copy constructors (the = operator) can be used for objects with non-pointer members.
When it has a pointer member though, you must use a programmer-defined copy constructor.
- General constructor define format is <object>::<object> (<object> &obj)
General method define format is <type> <object>::<method name> (parameters)
- Copy constructor example:
NumberArray::NumberArray (NumberArray &obj) {…}
NumberArray first ( 3, 5);
NumberArray second = first; //copy constructor called
//versus second = first, where a copy constructor is not called, just assignment operator
Overloaded operator
- Overloading ‘=’ operator to do ‘second = first’ without having to call copy constructor.
- MoveOrder MoveOrder::operator=( const MoveOrder &rhsObject)
//returns MoveOrder object on the left of ‘=’ if you do
MoveOrder MoveOrder::operator=( const MoveOrder &rhsObject)
{

return *this; //returns value of the object
}
- ‘second = first’ now the data of first is copied into second without errors
- ****Arithmetic overloaded operators – review before test (page 729 of textbook or section
11.6)
Overloading stream insertion/extraction operators
- Add “ friend ostream& operator<<( ostream &os, const MoveOrder &rhsObj ); ” as friend in
class declaration
- Ostream &operator<<(ostream &os, const MoveOrder &obj)
{
os << obj.<member variable>;
(repeated above)
return os;
}
Linked Lists
- //first and second are reference parameters because they have to be modified
void Split(ListNode* head, ListNode* &first, ListNode* &second) {…}
- Head->next = NULL;
ListNode* secondPtr = new ListNode();
head->next = secondPtr; //secondPtr MUST be on left, because we are setting ‘head->next’ to
the new pointer, not setting ‘secondPtr’ to ‘NULL’

You might also like