0% found this document useful (0 votes)
43 views24 pages

Module 1 C++editted

Constructors in C++ are special methods that are invoked automatically during object creation. They have the same name as the class and are used to initialize objects of the class. Constructors can be default, parameterized, or take a single parameter. The scope resolution operator (::) is used to access variables and functions that are out of scope. Static class members, including data members and functions, are shared among all objects of the class. Inline functions help reduce function call overhead for small functions.

Uploaded by

Thendral
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
43 views24 pages

Module 1 C++editted

Constructors in C++ are special methods that are invoked automatically during object creation. They have the same name as the class and are used to initialize objects of the class. Constructors can be default, parameterized, or take a single parameter. The scope resolution operator (::) is used to access variables and functions that are out of scope. Static class members, including data members and functions, are shared among all objects of the class. Inline functions help reduce function call overhead for small functions.

Uploaded by

Thendral
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 24

OOPS with C++

Constructor in C++
 Special method which invoke automatically during object creation
 Has same name as class name

 Syntax:

class_name(parameters list){
// constructor defintion
}
 Has no return type

 Constructor are declared within public section(optional)

 Overloaded Constructor is possible but inheritance is not possible.

 Types: 1.Default

2. parameterized
Example for default constructor
#include<iostream.h>
class myclass{ //class declaration
public:
myclass() //default constructor
{
cout<<“\n default constructor”;
}
};
int main(){
myclass obj1; //creating object 1;
myclass obj2; //creating object 2;
return 0;
}
OUTPUT:
default constructor //it is displayed when object1 is created
default constructor //it is displayed when object2 is created
Parameterized Constructors
 pass arguments to constructors.
 initialize an object when it is created.
 add parameters to it the way you would to any other

function.
 define the constructor's body use the parameters
to initialize the object
 Syntax: 1. Constructor_name obj_name(arguments);

 2. Class_name ob = obj_name (arguments);


#include<iostream.h>
class myclass { //class declaration
int a, b; //data members
public:
myclass(int i, int j) { //parameterized constructor
a=i; b=j;
}
void show() { // member function
cout << a << " " << b;
}
}; //class ends
int main() { //main function
myclass ob(3, 5); // alternate way to parameterized constructor
myclass ob = myclass(3, 4);
ob.show(); //calling member function
return 0;
}
OUTPUT:
Constructors with One Parameter:
 Incase of only one value to be passed as an arguments
 Syntax: Obj_name(values); or Obj_name=values;

 ob(i) or ob = i to initialize an object.


#include<iostream.h>
class X { //class declaration
int a; // data members
public:
X(int j)
{ a = j; // constructor with one parameter
}
int geta() {
return a; // member function
}
};
int main() {
X ob = 99; //assigning 99 value to obj
cout << ob.geta(); //calling member function
return 0;
}
outputs 99 //when ob.geta() is called 99 is assigned to
j hence 99 is printed
Constructor and Destructor
 Constructorinitializing object
 removing/destroying objects
 Starting and end of the main function
 Compliment of constructor is destructor
 Local objects: created when entering the block and destroyed when the block is left
 Global objects: created before main program begins and destroyed when the
program is left
 Uses of destructor: deallocate memory and deactivation of events
Constructor vs Destructor
class myclass { //Class declaration
public:
int who;
myclass(int id); //Constructor
~myclass(); //Destructor
} glob_ob1(1), glob_ob2(2); //global objects

myclass::myclass(int id) {
cout << "Initializing " << id << "\n"; who = id;
}

myclass::~myclass() {
cout << "Destructing " << who << "\n";
}
int main() {
myclass local_ob1(3); //local objects
cout << "This will not be first line displayed.\n";
myclass local_ob2(4);
return 0;
}
OUTPUT:
 Initializing 1
 Initializing 2

 Initializing 3

 This will not be first line displayed.

 Initializing 4

 Destructing 4

 Destructing 3

 Destructing 2

 Destructing 1

Explanation:
 1.After class creation two global objects, glob_ob1 and glob_ob2, are created outside any function. They are

instances of myclass with initial values 1 and 2.When these objects are created, the constructor is called for
each, and “Initializing 1” and “Initializing 2” will be printed.
 2.The constructor initializes the who member with the provided id and prints a message indicating the

initialization.
 3. The destructor prints a message indicating the destruction of the object and the value of who.

 4. Inside the main function, two local objects, local_ob1 and local_ob2, are created with initial values 3 and

4.
 5. When these local objects go out of scope (at the end of the main function), their destructors are called,

printing “Destructing 3” and “Destructing 4”.


The Scope Resolution Operator
 to reference the global variable or member function
that is out of scope.
 to access the hidden variable or function of a program
 double colon (::) symbol.
Uses of the scope resolution Operator
 to access the hidden variables or member functions of
a program.
 defines the member function outside of the class using

the scope resolution.


 to access the static variable and static function of a

class.
 to override function in the Inheritance.
#include <iostream.h>

// declare global variable


int num = 50;

int main ()
{
// declare local variable
int num = 100;

// print the value of the variables


cout << " The value of the local variable num: " << num;

// use scope resolution operator (::) to access the global variable


cout << "\n The value of the global variable num: " << ::num;
return 0;
}
Static Data Members
 declared using the static keyword.
 only one copy of the static data member in the class, even if
there are many class objects
 always initialized to zero when the first class object is created.

Syntax:static data_type data_member_name;


#include <iostream.h>
#include<string.h>

class Student {
private: int rollNo;
char name[10];
int marks;
public:
static int objectCount;
Student() {
objectCount++; }
void getdata() {
cout << "Enter roll number: "<<endl;
cin >> rollNo;
cout << "Enter name: "<<endl;
cin >> name;
cout << "Enter marks: "<<endl;
cin >> marks;
}
void putdata() {
cout<<"Roll Number = "<< rollNo <<endl;
cout<<"Name = "<< name <<endl;
cout<<"Marks = "<< marks <<endl;
cout<<endl;
} };
int Student::objectCount = 0;
int main(void) {
Student s1;
s1.getdata();
s1.putdata();

Student s2;
s2.getdata();
s2.putdata();

cout << "Total objects created = " <<


Student::objectCount << endl;
return 0;
}
Static Member Function
 static is a keyword to define static member function
inside and outside of the class.
 shares the single copy of the member function to any

number of the class' objects.

 SYNTAX class_name::function_name (parameter);


#include <iostream>
class Note
{
static int num;
public:
static int func ()
{
return num;
}
};
int Note :: num = 5;

int main ()
{
cout << " The value of the num is: " << Note:: func () << endl;
return 0;
}
Inline function
 to reduce the function call overhead.
 increase efficiency if it is small
 any changes made to an inline function will

require the inline function to be recompiled


again

 Syntax:
inline return_type function_name(parameters)
{
 // function code }
Not used in
 loop. (for, while, do-while)
 if a function has static variables.
 function recurses.
 If the return statement is absent from the

function body and the return type of the


function is not void.
 Whether a function uses a goto or switch

statement.
#include <iostream>

inline int cube(int s) {


return s * s * s;
}
int main()
{
cout << "The cube of 3 is: " << cube(3) << "\n";
return 0;
}
#include <iostream.h>

class Student {
public: double marks;

Student(double m) {
marks = m;
}
};

void calculateAverage(Student s1, Student s2)


{

double average = (s1.marks + s2.marks) / 2;


cout << "Average Marks = " << average << endl;
}
int main() {
Student student1(88.0),
student2(56.0);

calculateAverage(student1, student2);
return 0;
}
#include <iostream.h>
class Student {
public: double
marks1, marks2;
};
Student createStudent() {
Student student;
student.marks1 = 96.5;
student.marks2 = 75.0;
cout << "Marks 1 = " << student.marks1 << endl;
cout << "Marks 2 = " << student.marks2 << endl;
return student;
}
int main() {
Student student1;
student1 = createStudent();
return 0;
}

You might also like