Skip to document
This is a Premium Document. Some documents on Studocu are Premium. Upgrade to Premium to unlock it.

Notes C++

NOTES
Course

object oriented programming with c++ (BCS306B)

9 Documents
Students shared 9 documents in this course
Academic year: 2023/2024
Uploaded by:
Anonymous Student
This document has been uploaded by a student, just like you, who decided to remain anonymous.
Visvesvaraya Technological University

Comments

Please sign in or register to post comments.

Preview text

Semester :III

Subject :Object Oriented

Programming Concepts

Subject Code : BCS306B

Compiled by:

Department of CSE

RNSIT

An Overview of C++

What is object oriented programming

A program can be organized in one of two ways:

around its code (what is happening) or

around its data (who is being affected).

Main intention of OOP is to achieve data security.

In structured programming techniques, programs are typically organized around code. This

approach can be thought of as "code acting on data."

Ex: a program written in a structured language such as C is defined by its functions, any of

which may operate on any type of data used by the program.

Object-oriented programs are organized around data, with the key principle being "data

controlling access to code."

In an object-oriented language, data is given preference first and the routines that are

permitted to act on that data.

All OOP languages have three traits in common:

Encapsulation, Polymorphism and Inheritance

Encapsulation

Encapsulation is the mechanism that binds together code and the data it manipulates,

Encapsulation keeps data and code both safe from outside interference and misuse.

When code and data are linked together, an object is created. An object is a variable of a

user-defined type.

Within an object, code, data, or both may be private to that object or public.

Private code or data may not be accessed by a piece of the program that exists outside the

object. Public code or data can be accessed even though it is defined within an object.

Public parts of an object are used to provide a controlled interface to the private elements of

the object.

However, through the use of classifications, an object need only define those qualities that

make it unique within its class.

It is the inheritance mechanism that makes it possible for one object to be a specific instance

of a more general case.

The General Form of a C++ Program

C++ programs will have this general form:

#includes

base-class declarations

derived class declarations

nonmember function prototypes

int main( )

{

//...

}

nonmember function definitions

In large projects, all class declarations will be put into a header file and included with each

module. But the general organization of a program remains the same.

Ex:

#include <iostream>

using namespace std;

int main()

{

cout<<"Hello World";

return 0;

}

<iostream> : input output stream

Need for execution of cout statement

Namespace

Block of code in C++ can be named. Block of code can contain declaration statements or

definition statements.

Main necessity of namespaces is to avoid name clashes between identifiers. Two different

identifiers in two different namespaces can have the same name.

“std” is one such built-in namespace. User-defined namespaces can also be created in C++.

cout

cout is an object/variable of type ostream class in C++. Main purpose of cout is to print the

contents on to the standard output device.

<< is termed as insertion operator

Program to accept and print an integer value.

#include <iostream>

using namespace std;

int main()

{

cout<<"Enter an integer value\n";

int a;

cin>>a;

cout<<"Value of a is "<<a;

return 0;

}

cin is an object of type istream class in C++. >> is an extraction operator to read values from

standard input device. Variable definition can be done in any part of the program in C++.

Introducing C++ Classes

A class is similar syntactically to a structure. Class is a user-defined data type.

Class contains within it variables (data members) and functions (member functions), termed as

encapsulation. Data-members hold information and Member-functions are there to modify the

information in a logical manner. Since, OOP is designed to achieve data security

data-members will be hidden inside class scope and member-functions are allowed to be

accessed outside the class. Member-functions in turn will access data-members.

In order to achieve data security, three access specifiers are used in C++

1. private 2. protected 3. public

These access specifiers must be used within class / structure constructs in C++. There are 3

types of scopes in C++. 1. Global scope 2. Local scope 3. Class scope

No data-member can be declared as auto, extern or register.

Scope resolution operator

The :: operator links a class name with a member name in order to tell the compiler which

class the member belongs to.

It can also be used to access a global identifier, which exactly matches the name of local

identifier.

Ex: #include <iostream>

using namespace std;

int i=120;

int main()

{

int i=100;

cout<<i<<endl; //

cout<<::i<<endl; //

return 0;

}

Defining member functions outside the class

Member functions of a class can be defined either inside the class or outside class.

If a member function is defined inside the class it will be considered as an inline-function.

If a member function is defined outside the class, it will be considered as a

non-inline-function.

Syntax to define member function outside the class

<return-type> <class-name>:: <function-name> (Parameter list)

{

.....

}

‘::’ is termed as scope-resolution operator in C++.

# include <iostream>

using namespace std;

class complex

{

private: int r, i;

public:

void accept( );

void display( ); // Declaration of member functions

};

void complex::accept( )

{

cin>>r>>i;

}

void complex::display( )

{

cout<<r<<"+i"<<i;

}

int main( ) {

complex c1;

c1( );

c1( ); }

If no access specifier is associated with DM / MF’s then by default it is private.

Program to accept student information and to print it.

# include <iostream>

using namespace std;

# include <string>

class student

{

char nm[20];

char usn[20];

int marks[3];

public:

void get_values(char *, char *, char * );

void put_values( ); // Declaration of member functions

};

void student::get_values(char *n, char *u, char *m )

{

Since, put_values( ) is a member-function, it has a ‘this’ pointer. Whenever a member of a

class (i DM /MF) is accessed inside member-function, by default each member of the class

will be prefixed by “this->” automatically by the compiler.

Programmers can prefix each access of the member of the class by “this->” if needed.

Program to demonstrate ‘this’ holds the address of the invoking object.

# include <iostream>

using namespace std;

class complex

{

private: int r, i;

public:

void access( );

};

void complex::access( )

{

cout<<"\nContent of "this" is "<<this<<endl;

this->r = 10;

cout<<this->r;

}

int main( ) {

complex c1;

cout<<"Address of c1 is "<<&c1;

c1( );

}

Friend function

In C++ non member functions can also gain access to hidden private data members of the

class. If any non-member function is defined as a friend function, then it can access the

hidden members of the class.

Member-functions in C++ will access members of the class via this pointer.

Friend functions will not have this pointer, the reason being they are non-member functions.

Within the scope of the friend function, members of the class can be accessed using explicit

object names, which are passed to the scope of the friend functions using parameter passing

techniques.

Rules to code a friend function:

1. Friend function is declared, by prefixing the friend keyword for a function.

2. Friend function must be first declared inside the scope of the class and compulsorily

defined outside the class without a scope-resolution operator.

Declaration of friend function, must match exactly the definition of it.

3. Friend function will not have ‘this’ pointer.

4. Friend function can be called like a normal function invocation. Cannot use invoking

object to call a friend function.

5. From, friend function other member-functions can also be invoked.

Program to accept a complex value and to print it using friend function.

# include <iostream>

using namespace std;

class complex

{

private: int r, i;

public:

friend void accept(complex * );

friend void display(complex *);

};

void accept(complex * p )

{

cout<<"Enter r and i value\n";

cin>>p->r>>p->i;

}

void display(complex * p )

{

cout<<p->r<<"+i"<<p->i<<endl;

}

int main( ) {

complex c1;

accept(&c1);

display(&c1); }

saved when a function is called, and then restored when the function returns. The trouble is

that these instructions take time.

However, when a function is expanded in line, none of those overhead operations occur.

Although expanding function calls in line can produce faster run times, it can also result in

larger code size because of duplicated code. For this reason, it is best to inline only very small

functions. Further, it is also a good idea to inline only those functions that will have a

significant impact on the performance of the program.

In C++, short functions can be created that are not actually called; rather, their code is

expanded in line at the point of each invocation. This process is similar to using a

function-like macro.

To cause a function to be expanded in line rather than called, precede its definition with the

inline keyword.

Like the register specifier, inline is actually just a request, not a command, to the compiler.

The compiler can choose to ignore it. Also, some compilers may not inline all types of

functions.

Ex: it is common for a compiler not to inline a recursive function.

Remember, if a function cannot be inlined, it will simply be called as a normal function.

If a member-function is defined inside the class, by default it is considered as an

inline-function.

Dynamic memory allocation

In C++ operator ‘new’ is used for acquiring dynamic memory and operator ‘delete’ is used to

release the dynamically allocated memory.

Ex: int *p= NULL;

p = new int(10); // initializing dynamically acquired memory

// p = new int; without initializing dynamically acquired memory

cout<<*p;

delete p;

DMA for array

#include <iostream>

using namespace std;

int main( ) {

int *p= NULL;

int n,i;

cout<<"enter value for n\n";

cin>>n;

p = new int[n];

cout<<"Enter "<<n<<" values\n";

for(i=0;i<n;i++)

cin>>p[i];

cout<<"Array values are\n";

for(i=0;i<n;i++)

cout<<p[i]<<endl;

delete []p;

}

Passing objects to functions

Objects are passed to functions in just the same way that any other type of variable can, using

Call by value or Call by pointer or Call by reference technique.

Objects can also be passed to functions through the use of the standard call-by-value

mechanism. This means that a copy of an object is made when it is passed to a function.

In call-by-value mechanism, value is passed from actual parameter to formal parameter.

Formal parameter is an object, for which memory will be allocated first and then value will be

initialized to it via actual parameter. When memory is acquired to a formal parameter of type

object, constructor will be called and then value will be initialized to formal parameter. Once

the control is about to leave the called function, memory for local objects will be de-allocated,

which forces the destructor to be called.

Above functionality cannot be experienced, when Call-by-pointer or Call-by-reference

technique is used.

#include <iostream>

using namespace std;

class student

{

char name[20],usn[20];

int m[3];

public:

void accept()

{

cout<<"Enter name, usn and marks for 3 subjects\n";

cin>>name;

cin>>usn;

cin>>m[0]>>m[1]>>m[2];

}

void display()

{

cout<<"Name :"<<name<<endl;

cout<<"USN : "<<usn<<endl;

cout<<"Marks :"<<m[0]<<" "<<m[1]<<" "<<m[2]<<endl;

}

};

void swap(student &, student &);

int main()

{

student s1,s2;

s1(); s2();

cout<<"*** Before swapping\n";

s1(); s2();

swap(s1,s2);

cout<<"*** After swapping\n";

s1(); s2();

return 0;

}

void swap(student &r, student &s)

{

student t;

t = r;

r = s;

s = t;

}

Constructor

Constructor is a member function, task is to initialize data-members of the class.

Constructor member function name must match the class name.

Constructor need not be invoked explicitly, it will be called automatically once memory for

an object is acquired or an object is created.

Constructor sole purpose is to initialize data-members of the class.

Since, constructor is a member function, it has a ‘this’ pointer in its scope.

Types of constructor in C++: 1) Zero parameterized constructor

2) Parameterized constructor 3) Copy constructor

There is no return type for constructor, not even void.

Zero parameterized constructor

ZPC is a type of constructor, where formal parameters will not be present.

#include <iostream>

using namespace std;

class complex

{

private: int r, i;

public:

complex ( )

{ cout<<"*** ZPC called ***\n"; }

};

int main( ) {

cout<<"Object created: "; complex p1; // ZPC called

cout<<"Pointer created \n"; complex *p=NULL; // ZPC not called

Constructor with one parameter: A special case

Copy constructor

CC can be used to initialize one object with another.

An object can be initialized with another without using CC, termed as bitwise copy. Each

member’s value in the source object will be transferred to the same member in the destination.

That is, an identical copy of the initializing object is created in the target object.

Ex: complex c1(1,2);

complex c2 = c1; // all values of c1 will be transferred to c2.

There will be some logical situations, where bitwise copy may not be used.

Consider the complex class as declared below

class complex

{

int *r,*i;

public:

complex(int , int );

void display( );

};

First step will be to create an object, say c1, where data members r and i are pointers, without

placing valid addresses in r and i, values cannot be stored in them. Task of acquiring dynamic

memory and placing valid addresses in r and i can be done in a parameterized constructor.

After placing valid addresses in r and i value can be stored in the location pointed by r and i.

Further, if another object of same class, say c2, need to be initialized by c1, statement will be

as follows: complex c2 = c1;

Without an explicitly defined copy constructor, two different objects will be pointing to the

same dynamically acquired memory.

General form of copy constructor

<class-name>(const <class-name> & <alias-name> )

{

......

}

Ex: Without copy constructor

#include <iostream>

using namespace std;

class complex { //without Copy Constructor

int *r,*i;

public:

complex(int , int );

void display( ); };

complex :: complex(int p, int q) {

r = new int(p);

i = new int(q); }

void complex::display( ) {

cout<<"Content of r is "<<r<<endl;

cout<<"Content of i is "<<i<<endl;

cout<<"Value pointed by r is "<<*r<<endl;

cout<<"Value pointed by i is "<<*i<<endl; }

int main() {

complex c1(10,20);

complex c2 = c1;

cout<<"**** Object c1 ****\n"; c1();

cout<<"**** Object c2 ****\n"; c2();

return 0; }

Using Copy constructor

#include <iostream>

using namespace std;

class complex

{

int *r,*i;

public:

complex(int , int );

complex(const complex &);

Was this document helpful?
This is a Premium Document. Some documents on Studocu are Premium. Upgrade to Premium to unlock it.

Notes C++

Course: object oriented programming with c++ (BCS306B)

9 Documents
Students shared 9 documents in this course
Was this document helpful?

This is a preview

Do you want full access? Go Premium and unlock all 116 pages
  • Access to all documents

  • Get Unlimited Downloads

  • Improve your grades

Upload

Share your documents to unlock

Already Premium?
Semester :III
Subject :Object Oriented
Programming Concepts
Subject Code : BCS306B
Compiled by:
Department of CSE
RNSIT

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.