Search for notes by fellow students, in your own course and all over the country.

Browse our notes for titles which look like what you need, you can preview any of the notes via a sample of the contents. After you're happy these are the notes you're after simply pop them into your shopping cart.

My Basket

You have nothing in your shopping cart yet.

Title: C++ Lecture note 1
Description: General concepts about object oriented programming,class,objects,reference variable,inline function,function overloading.

Document Preview

Extracts from the notes are below, to see the PDF you'll receive please use the links above


COMP 116
Object Oriented Programming
Lecture 1

Outline
Introducing C++
• Introduction
• A sample C++ program
• Reference Variables
• Inline Functions
• Function Overloading
• Comparison between C and C++
2

Outline contd…
Introduction to Object Oriented Programming
• Concept of Object Oriented Paradigm
• Features of OOP
• Benefits of OOP

3

Introducing C++
• OOP developed by Bjarne Stroustrup at AT&T Bell
laboratories in 1980s
• Combination of Simula67 and C
• Classes, inheritance, function overloading, and operator
overloading are the most important features in C++

4

Introducing C++ Contd…
• Hello World!! Program in C
#include ...
Your Roll no
...
Your rollno is "<}

7

A Sample C++ Program
#include
using namespace std;
main()
{
int rollno;
char name[15];
cout<<"Enter your name ";
cin>>name;
cout<<"Enter your Roll number ";
cin>>rollno;
cout<<"Hello "< ...

• The standard input & output functions differ in the two languages
C uses scanf & printf while C++ uses cin>> & cout<< as their respective
input & output functions

9

Differences between C and C++
contd…
• Declarations
– In C global variables can be declared several times without using
extern specifier
...
This can be done only in C++ with the help of Polymorphism(an
OOP feature)
• Inline functions
inline int add(int a, int b)
{
return (a+b);
}
• Default function arguments
Void myFunction(int a, int b=5, int c=10);

11

Differences between C and C++
contd…
#include
using namespace std;
void f(int x = 0, int y = 100);
int main()
{
f(1, 2);
f(10);
f();
return 0;
}
void f(int x, int y)
{
cout << "x: " << x << ", y: " << y << "\n";
}

12

Differences between C and C++
contd…
• Declarations
– In C, all declarations should be made at the top
...

#include
using namespace std;
int n = 12; // A global variable
int main() {
int n = 13; // A local variable
cout << ::n << endl; // Print the global variable: 12
cout << n << endl; // Print the local variable: 13
}
13

Lab Work
• Assignment
– Assignment 1

• Lab Manual
– Lab 1

14

Reference Variables
void swap(float&,float&);
int main()
{
float a = 22
...
4;
cout << "a = " << a << ", b = " << b << "\n";
swap(a,b);
cout << "a = " << a << ", b = " << b << "\n";
}
void swap(float& x,float& y)
{
// exchanges the values of x and y:
float temp = x;
x = y;
y = temp;
}

15

Reference Variables contd…
• There are some situations where a function needs to change
the value of the parameter passed to it
...

• To pass a parameter by reference instead of by value, simply
append an ampersand, &, to the type specifier in the
functions parameter list
...

• So the argument is read-write instead of read-only
...


16

Reference Variables contd…
• The parameters that are passed by value are called value
parameters, and parameters that are passed by reference are
called reference parameters
...

• When the call swap(a,b) executes, the function creates its
local references x and y, so that x is the function’s local name
for a, and y is the function’s local name for b
...

• But the compiler will accept:
– float& x, float &x, float & x, or even float&x
...


The parameter x is a local reference
...


It is a synonym for the argument
...


It can change the argument
...


The argument passed by reference
must be a variable
...


The argument is read-write
...

• When a function has to return more than one value we use
reference parameters
...

– It works the same way as passing by reference, except that
the function is prevented from changing the value of the
parameter
...


22

Reference Variables contd…
void f(int,int&,const int&);
int main()
{
int a = 22,b = 33, c = 44;
cout << "a = " << a << ", b = " << b << ", c = " << c<<"\n";
f(a,b,c);
cout << "a = " << a << ", b = " << b << ", c = " << c<<"\n";
f(2*a-3,b,c);
cout << "a = " << a << ", b = " << b << ", c = " << c<<"\n" ;
}
void f(int x, int& y, const int& z)
{
x += z;
y += z;
cout << "x = " << x << ", y = " << y << ", z = " << z<<"\n" ;
}

23

Inline Function
• What is inline function?
• Need of Inline function/Advantage
• How to create Inline function in C++?

24

Inline Function
• Simple function
• Same as used to do in a C
• Only difference is it consist of keyword Inline
inline void sum(int a, int b) // used in function definition
• Inline keyword will convert function in to inline
function
• It is just a request and not a order

25

Inline Function [contd…
...
of times
Takes amount of time storing, jumping and manipulating
information
26

Inline Function [contd…
...
of arguments must be changed
– Types of arguments must be changed
– Sequence of arguments must be changed

return type has no significance in function
overloading

34

Introduction to Object Oriented
Programming

• Concept of Object Oriented Paradigm
• Features of OOP
• Benefits of OOP

35


Title: C++ Lecture note 1
Description: General concepts about object oriented programming,class,objects,reference variable,inline function,function overloading.