[go: up one dir, main page]

0% found this document useful (0 votes)
29 views37 pages

Intro To Class and C++

This document discusses key concepts of classes in C++ including class declaration, private and public members, constructors, destructors, parameterized constructors, object pointers, object assignment, arrays of objects, the THIS pointer, objects as function parameters, and returning objects. It provides code examples to demonstrate these class concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views37 pages

Intro To Class and C++

This document discusses key concepts of classes in C++ including class declaration, private and public members, constructors, destructors, parameterized constructors, object pointers, object assignment, arrays of objects, the THIS pointer, objects as function parameters, and returning objects. It provides code examples to demonstrate these class concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

CSE 109

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

• Error: cannot access private member


Public Member
• public keyword used
Public & Private Member
• public keyword used
Member Function
• General form:
ret-type class-name::func-name(parameter-list)
{
//function body
}
Automatic in-lining
• Member function definition inside class declaration
• inline keyword not necessary
• Using the keyword will generate compile error
Constructor
• Function called automatically each time a object is created
– Object created when the declaration statement for the object is executed
• Can be used for initialization
• Has the same name as the class name
• Has no return type
– Illegal to have return type
• Default constructor
Constructor

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 object_array4_using pointer.cpp


THIS Pointer
• A special pointer
• Automatically passed to any member function when it is called
• Pointer to the object that generates the call
THIS 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 ☺

You might also like