0% found this document useful (0 votes)
18 views

Unit 3 - C-- Clas

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

Unit 3 - C-- Clas

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

UNIT 3

(https://www.geeksforgeeks.org/introduction-of-object-oriented-programming/)

Introduction of Object Oriented Programming

OOPs Concepts:

• Class

• Objects

• Data Abstraction

• Encapsulation

• Inheritance
• Polymorphism

• Dynamic Binding

• Message Passing
Benefits of OOP

Simple C++ program

Structure:

// In C++ We can Initialize the Variables with Declaration in Structure.

#include <iostream>

using namespace std;

struct Point {

int x = 0; // It is Considered as Default Arguments and no Error is Raised

int y = 1;

};

int main()

struct Point p1;

// Accessing members of point p1

// No value is Initialized then the default value is considered. ie x=0 and y=1;

cout << "x = " << p1.x << ", y = " << p1.y<<endl;

// Initializing the value of y = 20;

p1.y = 20;

cout << "x = " << p1.x << ", y = " << p1.y;

return 0;

}
C++ Classes and Objects

• What is a Class in C++?

class ClassName {
access_specifier:
// Body of the class
};

Example
class ThisClass {
public:
int var; // data member
void print() { // member method
cout << "Hello";
}
};

Example:
class ThisClass {
public:
int var; // data member
int print() { // member method
cout << var;
}
};
int main()
{
ThisClass t,t1,t2;//object creation
t.var=10;
t1.var=20;
t2.var=30;
t.print();//10
t1.print();//20
t2.print();//30
}
What is an Object in C++?

Syntax:

ClassName ObjectName;

Example:

MyClass obj;

Accessing Data Members and Member Functions

obj.printName()

example:

// C++ program to illustrate how create a simple class and


// object
#include <iostream>
#include <string>
using namespace std;
// Define a class named 'Person'
class Person {
public:
// Data members
string name;
int age;

// Member function to introduce the person


void introduce()
{
cout << "Hi, my name is " << name << " and I am "
<< age << " years old." << endl;
}
};

int main()
{
// Create an object of the Person class
Person person1, person2,person3;

// accessing data members


person1.name = "Alice";
person1.age = 30;
person2.name=”XXX”;
person2.age=40;
person3.name=”sai”;
person3.age=20;

// Call the introduce member method


person1.introduce();
person2.introduce();
person3.introduce();
return 0;
}

Nesting of member functions –


Illustration 14.7 The use of Nesting of Member Function
A member function can call another member function of the same class .
#include<iostream>
using namespace std
class nest
{
int a;// data member
int square_num( )//member function
{
return a* a;
}
public:
void input_num( )
{
cout<<”\nEnter a number ”;
cin>>a;3
}
int cube_num( )
{
return a* a*a;
}
void disp_num()
{
int sq;
sq=square_num(); //nesting of member function//9
int cu;
cu=cube_num(); //nesting of member function//27
cout<<”\nThe square of “<<a<<” is ” <<sq;9
cout<<”\nThe cube of “<<a<<” is ” <<cu;27
}
};
int main()
{
nest n1;
n1.input_num();
n1.disp_num();
return 0;
}
Output:
Enter a number 5
The square of 5 is 25
The cube of 5 is 125

In the above program disp_num() function calls two other member


function square_ num() and cube_num(). Both are defined in different visibility mode

Private Member Function

#include <iostream>
using namespace std;

class Student {
private:
int rNo;
float perc;
//private member functions
void inputOn(void)
{
cout << "Input start..." << endl;
}
void inputOff(void)
{
cout << "Input end..." << endl;
}

public:
//public member functions
void read()
{
//calling first member function
inputOn();
//read rNo and perc
cout << "Enter roll number: ";
cin >> rNo;
cout << "Enter percentage: ";
cin >> perc;
//calling second member function
inputOff();
}
void print(void)
{
cout << endl;
cout << "Roll Number: " << rNo << endl;
cout << "Percentage: " << perc << "%" << endl;
}
};

//Main code
int main()
{
//declaring object of class student
Student std;

//reading and printing details of a student


std.read();
std.print();

return 0;
}

Memory allocation for objects


include <iostream>
using namespace std;
//The members will be allocated with memory space only after the creation of the class type object
class product
{
int code, quantity;
float price;

public:
void assignData();
void Print();
};
int main()
{
product p1, p2;
cout<<”\n Memory allocation for object p1 ” <<sizeof(p1);
cout<<”\n Memory allocation for object p2 ” <<sizeof(p2);
return 0;
}
Output:
Memory allocation for object p1 12
Memory allocation for object p2 12
Static Data Members
Syntax:
className {
static data_type data_member_name;// static int x;
.....
};

datatype class_name::var_name = value...;


ex:
int A::x = 10

// C++ Program to demonstrate the use of


// static data members
#include <iostream>
using namespace std;

// class definition
class A {
public:
// static data member here
static int x;
A() // constructor is created
{
cout << "A's constructor called " << endl;
}
};

int A::x = 2;

int main()
{
// accessing the static data member using scope
// resultion operator
cout << "Accessing static data member: " << A::x
<< endl;

return 0;
}

Accessing a Static Member


1. Accessing static data member using Class Name and Scope Resolution Operator
Class_Name :: var_name
A::x

2. Accessing static data member through Objects


object_name . var_name
obj.x

Static Member Function


class Person{
static int index_number;
};

Example:
// C++ Program to demonstrate
// Static member in a class
#include <iostream>
using namespace std;

class Student {
public:
// static member
static int total;

// Constructor called
Student() {
total += 1;
}
};

int Student::total = 0;

int main()
{
// Student 1 declared
Student s1;//total=0+1=1
cout << "Number of students:" << s1.total << endl;1

// Student 2 declared
Student s2;//total=1+1=2
cout << "Number of students:" << s2.total << endl;//2

// Student 3 declared
Student s3;//total=2+1=3
cout << "Number of students:" << s3.total << endl;//3
return 0;
}
Array of Objects
ClassName ObjectName[number of objects];

// C++ program to implement


// the above approach
#include<iostream>
using namespace std;

class Employee
{
int id;
char name[30];
public:
void getdata();//Declaration of function
void putdata();//Declaration of function
};
void Employee::getdata(){//Defining of function
cout<<"Enter Id : ";
cin>>id;
cout<<"Enter Name : ";
cin>>name;
}
void Employee::putdata(){//Defining of function
cout<<id<<" ";
cout<<name<<" ";
cout<<endl;
}
int main(){
Employee emp; //One member
emp.getdata();//Accessing the function
emp.putdata();//Accessing the function
return 0;

}
int main()
{
// This is an array of objects having
// maximum limit of 30 Employees
Employee emp[30];
int n, i;
cout << "Enter Number of Employees - ";
cin >> n;

// Accessing the function


for(i = 0; i < n; i++)
emp[i].getdata();//emp[0].getdata
emp[1].getdata()
emp[2].getdata()

cout << "Employee Data - " << endl;

// Accessing the function


for(i = 0; i < n; i++)
emp[i].putdata();
}

Objects as Function Arguments in c++

#include<iostream.h>
class weight {
int kilogram;
int gram;
public:
void getdata ();
void putdata ();
void sum_weight (weight,weight) ;
};
void weight :: getdata() {
cout<<"/nKilograms:";
cin>>kilogram;
cout<<"Grams:";
cin>>gram;
}
void weight :: putdata () {
cout<<kilogram<<" Kgs. and"<<gram<<" gros.\n";
}
void weight :: sum_weight(weight w1,weight w2) {
gram = w1.gram + w2.gram;
kilogram=gram/1000;
gram=gram%1000;
kilogram+=w1.kilogram+w2.kilogram;
}
int main () {
weight w1,w2 ,w3;
cout<<"Enter weight in kilograms and grams\n";
cout<<"\n Enter weight #1" ;
w1.getdata();kilogram,gram
cout<<" \n Enter weight #2" ;
w2.getdata();kilogram,gram
w3.sum_weight(wl,w2);
cout<<"/n Weight #1 = ";
w1.putdata();
cout<<"Weight #2 = ";
w2.putdata();
cout<<"Total Weight = ";
w3.putdata();
return 0;
}
Returning Objects in C++
https://www.geeksforgeeks.org/passing-and-returning-objects-in-c/

Passing an Object as argument

function_name(object_name);

// C++ program to show passing


// of objects to a function

#include <bits/stdc++.h>
using namespace std;

class Example {
public:
int a;

// This function will take


// an object as an argument
void add(Example E)
{
a = a + E.a;//a=100+50=150
}
};

// Driver Code
int main()
{
// Create objects
Example E1, E2;

// Values are initialized for both objects


E1.a = 50;
E2.a = 100;
cout << "Initial Values \n";
cout << "Value of object 1: " << E1.a
<< "\n& object 2: " << E2.a
<< "\n\n";

// Passing object as an argument


// to function add()
E2.add(E1);//E2.add(50)

// Changed values after passing


// object as argument
cout << "New values \n";
cout << "Value of object 1: " << E1.a
<< "\n& object 2: " << E2.a
<< "\n\n";
50
150
return 0;
}
Returning Object as argument
object = return object_name;

// C++ program to show passing


// of objects to a function

#include <bits/stdc++.h>
using namespace std;

class Example {
public:
int a;

// This function will take


// object as arguments and
// return object
Example add(Example Ea, Example Eb)
{
Example Ec;
Ec.a = Ea.a + Eb.a;//50+100

// returning the object


return Ec;//150
}
};
int main()
{
Example E1, E2, E3;

// Values are initialized


// for both objects
E1.a = 50;
E2.a = 100;
E3.a = 0;

cout << "Initial Values \n";


cout << "Value of object 1: " << E1.a
<< ", \nobject 2: " << E2.a
<< ", \nobject 3: " << E3.a
<< "\n";

// Passing object as an argument


// to function add()
E3 = E3.add(E1, E2);

// Changed values after


// passing object as an argument
cout << "New values \n";
cout << "Value of object 1: " << E1.a
<< ", \nobject 2: " << E2.a
<< ", \nobject 3: " << E3.a
<< "\n";

return 0;
}

Friend Class and Function in C++

friend class class_name;

// C++ Program to demonstrate the


// functioning of a friend class
#include <iostream>
using namespace std;
class GFG {
private:
int private_variable;

protected:
int protected_variable;

public:
GFG()
{
private_variable = 10;
protected_variable = 99;
}

// friend class declaration


friend class F;
};

// Here, class F is declared as a


// friend inside class GFG. Therefore,
// F is a friend of class GFG. Class F
// can access the private members of
// class GFG.
class F {
public:
void display(GFG& t)
{
cout << "The value of Private Variable = "
<< t.private_variable << endl;
cout << "The value of Protected Variable = "
<< t.protected_variable;
}
};

// Driver code
int main()
{
GFG g;
F fri;
fri.display(g);
return 0;
}

Friend Function
https://www.geeksforgeeks.org/friend-class-function-cpp/
A friend function can be:
1. A global function
2. A member function of another class
friend return_type function_name (arguments); // for a global function
or
friend return_type class_name::function_name (arguments); // for a member function of another
class

1. Global Function as Friend Function

// C++ program to create a global function as a friend


// function of some class
#include <iostream>
using namespace std;

class base {
private:
int private_variable;

protected:
int protected_variable;

public:
base()
{
private_variable = 10;
protected_variable = 99;
}

// friend function declaration


friend void friendFunction(base& obj);
};

// friend function definition


void friendFunction(base& obj)
{
cout << "Private Variable: " << obj.private_variable
<< endl;//10
cout << "Protected Variable: " << obj.protected_variable;//99
}

// driver code
int main()
{
base object1;
friendFunction(object1);

return 0;
}

2. Member Function of Another Class as Friend Function


// C++ program to create a member function of another class
// as a friend function
#include <iostream>
using namespace std;

class base; // forward definition needed


// another class in which function is declared
class anotherClass {
public:
void memberFunction(base& obj);
};

// base class for which friend is declared


class base {
private:
int private_variable;

protected:
int protected_variable;

public:
base()
{
private_variable = 10;
protected_variable = 99;
}

// friend function declaration


friend void anotherClass::memberFunction(base&);
};

// friend function definition


void anotherClass::memberFunction(base& obj)
{
cout << "Private Variable: " << obj.private_variable
<< endl;
cout << "Protected Variable: " << obj.protected_variable;
}

// driver code
int main()
{
base object1;
anotherClass object2;
object2.memberFunction(object1);

return 0;
}
Constructor:

<class-name> (list-of-parameters) {
// constructor definition
}

<class-name> {

// Declaring the constructor


// Definiton will be provided outside
<class-name>();

// Defining remaining class


}

<class-name>: :<class-name>(list-of-parameters) {
// constructor definition
}

You might also like