Unit 3 - C-- Clas
Unit 3 - C-- Clas
(https://www.geeksforgeeks.org/introduction-of-object-oriented-programming/)
OOPs Concepts:
• Class
• Objects
• Data Abstraction
• Encapsulation
• Inheritance
• Polymorphism
• Dynamic Binding
• Message Passing
Benefits of OOP
Structure:
#include <iostream>
struct Point {
int y = 1;
};
int main()
// No value is Initialized then the default value is considered. ie x=0 and y=1;
cout << "x = " << p1.x << ", y = " << p1.y<<endl;
p1.y = 20;
cout << "x = " << p1.x << ", y = " << p1.y;
return 0;
}
C++ Classes and Objects
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;
obj.printName()
example:
int main()
{
// Create an object of the Person class
Person person1, person2,person3;
#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;
return 0;
}
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;
.....
};
// 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;
}
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];
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;
#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/
function_name(object_name);
#include <bits/stdc++.h>
using namespace std;
class Example {
public:
int a;
// Driver Code
int main()
{
// Create objects
Example E1, E2;
#include <bits/stdc++.h>
using namespace std;
class Example {
public:
int a;
return 0;
}
protected:
int protected_variable;
public:
GFG()
{
private_variable = 10;
protected_variable = 99;
}
// 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
class base {
private:
int private_variable;
protected:
int protected_variable;
public:
base()
{
private_variable = 10;
protected_variable = 99;
}
// driver code
int main()
{
base object1;
friendFunction(object1);
return 0;
}
protected:
int protected_variable;
public:
base()
{
private_variable = 10;
protected_variable = 99;
}
// driver code
int main()
{
base object1;
anotherClass object2;
object2.memberFunction(object1);
return 0;
}
Constructor:
<class-name> (list-of-parameters) {
// constructor definition
}
<class-name> {
<class-name>: :<class-name>(list-of-parameters) {
// constructor definition
}