Professional Documents
Culture Documents
Oop Unit 1 Scoe Comp
Oop Unit 1 Scoe Comp
UNIT-I
Fundamentals of Object Oriented Programming
Classe syntax:
…
UNIT -I Object Oriented Programming 15
Fundamentals of object oriented
Programming
Methods:
• Methods are functions that belongs to the class.
• There are two ways to define functions that belongs
to a class:
– Inside class definition.
– Outside class definition.
class example
{
public:
void add() //method defined inside class
{
------
------
}
};
Int main()
{
example obj;
obj.add();
return 0;
}
class example
{
public:
void add() //method declared inside class
};
Int main()
{
example obj;
obj.add();
return 0;
}
Messages:
• Objects communicate with one another by sending
and receiving information to each other.
• A message for an object is a request for execution
of a procedure and therefore will invoke a method in
the receiving object that generates the desired
results.
e.g. employee.salary(name)
(object) (Message) (information)
Header section
Class declaration
and
operation/method definition section
Global declaration section
Main function
Other methods definition section
#include<iostream>
using namespace std;
class example
{
//Class declaration, if any along with its member variables and functions
};
//member function definition outside of class
//global declarations, if any
int main()
{
// Defining enum Gender
enum Gender { Male, Female };
0 1 2 3 4 5
Arrays Indices
Array Length : 6
First Index: 0
Last Index: 5
UNIT -I Object Oriented Programming 46
Strings
class Books
{
char title[50];
Data members
char author[50];
public:
void get_book();
void show_book(); Member functions
};
Abstraction means
displaying only
essential information
and hiding the
details.
int main()
{
MyClass myObj; // Object of MyClass
// function creation
void myFunction()
{
cout << "I just got executed!";
}
int main()
{
for (int i=0; i<10; i++)
{ myFunction(); } // call the function
return 0;
}
Output:
"I just got executed!"
};
int main()
{
MyClass myObj; // object of MyClass will call the constructor
return 0;
}
class MyClass
{
public:
MyClass() // default Constructor
{
Default constructor is cout << "Hello World!";
called with no }
arguments. };
int main()
{
MyClass myObj;
return 0;
}
class MyClass
{
public:
MyClass(int p) // parameterized Constructor
{
This constructor is -------
-------
called with arguments. }
};
int main()
{
MyClass myObj(10);
return 0;
}
class Point
{
private:
int x, y;
public:
Point(int x1, int y1)
int main()
{
{
x = x1; Point p1(10, 15);
y = y1; Point p2 = p1; // Copy constructor
} return 0;
// Copy constructor }
Point(const Point &p)
{
x = p.x;
y = p.y;
}
};
class Temperature
{ int celsius;
public:
Temperature()
{ celsius=0; int main()
} {
friend int temp(Temperature); Temperature tm;
}; cout<<temp(tm)<<endl;
return 0;
}
int temp(Temperature t)
{ t.celsius=40;
returnt.celsius;
}