0% found this document useful (0 votes)
24 views20 pages

OOPS With C++

The document provides an overview of Object-Oriented Programming (OOP) concepts in C++, including parameterized constructors, destructors, the scope resolution operator, static data members and functions, and inline functions. It includes code examples demonstrating the use of these features, such as initializing objects, accessing global variables, and calculating averages. The document emphasizes the importance of constructors and destructors in managing object lifecycle and memory.

Uploaded by

Antony Johny
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views20 pages

OOPS With C++

The document provides an overview of Object-Oriented Programming (OOP) concepts in C++, including parameterized constructors, destructors, the scope resolution operator, static data members and functions, and inline functions. It includes code examples demonstrating the use of these features, such as initializing objects, accessing global variables, and calculating averages. The document emphasizes the importance of constructors and destructors in managing object lifecycle and memory.

Uploaded by

Antony Johny
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

OOPS with C++

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
#include<iostream.h>
class myclass {
int a, b;
public:
myclass(int i, int j) {
a=i; b=j;
}
void show() {
cout << a << " " << b;
}
};
int main() {
myclass ob(3, 5); // myclass ob = myclass(3, 4);
ob.show();
return 0;
}
Constructors with One
Parameter:
 ob(i) or ob = i to initialize an object.
#include<iostream.h>
class X {
int a;
public:
X(int j)
{ a = j;
}
int geta() {
return a;
}
};
int main() {
X ob = 99; // passes 99 to j
cout << ob.geta(); // outputs 99
return 0;
}
Constructor and Destructor
 initializing object
 removing/destroying objects
 Starting and end of the main function
class myclass {
public:
int who;
myclass(int id);
~myclass();
} glob_ob1(1), glob_ob2(2);

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

myclass::~myclass() {
cout << "Destructing " << who << "\n";
}

int main() {
myclass local_ob1(3);
cout << "This will not be first line displayed.\n";
myclass local_ob2(4);
return 0;
}
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>

// 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(paramete
rs) {
 // 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