Intro To Class and C++
Intro To Class and C++
Computer Programming
Class
Prepared by
Madhusudan Basak
Assistant Professor
CSE, BUET
Modified by
Shashata Sawmya
Lecturer
CSE, BUET
Class
• class: keyword
• Declaration similar to structure
• General form
class class-name {
//private function and variables
public:
//public function and variables
} object-list;
• object-list is optional
• Member: functions and variables declared inside a class
• By default members are private
– Can not be accessed from outside
Class & Object Declaration
• Class Declaration:
class myclass {
int a;
};
– Defines new type, no storage created
• Object declaration:
– myclass ob1, ob2;
– Storage created
Private Member
• By default members are private
See constructor.cpp
Destructor
• Complement of a constructor
• Function called automatically when the object is destroyed
– Local object destroyed when goes out of scope
– Global object destroyed when program ends
• Function name is the name of the function preceded by ~
• Can not take parameters
Destructor
See destructor.cpp
Parameterized Constructor
See constructor_parameterized.cpp
Parameterized Constructor
• If default constructor is not available following statement will cause error
– myclass ob;
Object Pointers
See object_pointers.cpp
Object Assignment
See object_assignment.cpp
Object Assignment
• One object can be assigned to other if both are of same type
• Bitwise copy of members variables are done
• The two objects will remain completely separate
• It is not sufficient that the object types are physically similar
• Type name should be similar
Object Assignment
See object_assignment2.cpp
Arrays of Objects
• Objects can be arrayed
• Array objects can be initialized if constructor available
Arrays of Objects
See object_array1.cpp
Arrays of Objects
See object_array2.cpp
Arrays of Objects
See object_array3.cpp
Arrays of Objects
#include<iostream> int main()
using namespace std; {
class Point { Point ob[4]={
int x; Point(1, 3),
int y; Point(2, 4),
public: Point(3, 5),
Point(int a, int b) Point(4, 8)
{ };
x=a; return 0;
y=b; }
}
};
Object Pointer
• When an object pointer is incremented it points to the next object
Object Pointer
See this_pointer.cpp
THIS Pointer
See this_pointer2.cpp
Object as parameter don’t show this
See this_pointer3.cpp
Object as parameter
See object_in_function1.cpp
Object as parameter
• Parameter object is passed by value
• Bitwise copy of the argument made
• When copy of a object made constructor is not called (like assignment)
• When the function terminates the copy is destroyed
• When the copy is destroyed the destructor is called
• Changes to the object inside function do not effect the object
• Address of the object can be passed
Object as parameter
See object_in_function2.cpp
Object pointer as parameter
See object_in_function3.cpp
Returning Object
• Can be returned using normal return statement
• When an object is returned from a function a temporary object is created
automatically which holds the return value
• After the value has been returned the object is destroyed
• Careful if the object contains destructor function which frees memory
Returning Object
See object_in_function4.cpp
References
• Teach Yourself C++ by Herbert Schildt (Third Edition)
– Chapter 1 (1.5)
– Chapter 2 (2.1, 2.2, 2.4, 2.6, 2.7)
– Chapter 3 (3.1-3.3)
– Chapter 4 (4.1-4.3)
Thank You ☺