lec# 2 OOP.pptx - Object Oriented Programming(CSC-205...

This preview shows page 1 - 7 out of 13 pages.

Object Oriented Programming (CSC-205) Lecture: 02 Getting Familiar with Object Oriented Environment (Constructor and Destructor)
Agenda for Today To get familiar with the notions of constructors and destructors Implementation of multiple constructors Constructor overloading The concept of friend function
Constructors A constructor is a member function of a class that is called and executed automatically when an object of that class is created The name of the constructor function is the same as the name of the class itself A constructor function may have arguments but it cannot return any value
Quick Example #include<iostream> using namespace std; class test { public: test() {cout<<"Welcome"<<endl; } }; main() { test obj; }
Initializing Data Using Constructors The constructor functions are normally used to initialize values in data members of a class when the program is executed This type of initialization is called the automatic initialization
#include<iostream> using namespace std; class sum { private:

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture