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

05 Object Oriented Concept

The document provides an introduction to C++ programming, focusing on key concepts such as classes, objects, methods, access specifiers, constructors, destructors, and inheritance. It explains how to create classes and objects, use access specifiers, and implement encapsulation and polymorphism. Additionally, it covers function overloading and overriding, illustrating these concepts with code examples.

Uploaded by

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

05 Object Oriented Concept

The document provides an introduction to C++ programming, focusing on key concepts such as classes, objects, methods, access specifiers, constructors, destructors, and inheritance. It explains how to create classes and objects, use access specifiers, and implement encapsulation and polymorphism. Additionally, it covers function overloading and overriding, illustrating these concepts with code examples.

Uploaded by

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

Introduction To C++ Programming

What are Classes and Objects?

Classes and objects are the two main aspects of object-oriented


programming.
What are Classes and Objects?

Classes and objects are the two main aspects of object-oriented


programming.

Another Example:-
What are Classes and Objects?

Classes and objects are the two main aspects of object-oriented


programming.

Another Example:-

A class is a template for objects, and an object is an instance of a


class.
Classes/Objects

Everything in C++ is associated with classes and objects, along with its
attributes and methods

Attributes and methods are basically variables and functions that


belongs to the class. These are often referred to as "class members".
Create a Class

To create a class, use the class keyword:

class MyClass { // The class


public: // Access specifier
int myNum; // Attribute (int variable)
string myString; // Attribute (string
variable)
};
Create an Object

To Create an object of MyClass, specify the class name, followed by the object name.

int main() {
MyClass myObj; // Create an object of MyClass

// Access attributes and set values


class MyClass {
myObj.myNum = 15;
public:
myObj.myString = "Some text";
int myNum;
string myString;
// Print attribute values
};
cout << myObj.myNum << "\n";
cout << myObj.myString;
return 0;
}
Create an Multiple Object

To Create an object of MyClass, specify the class name, followed by the object name.
int main() {
MyClass myObj1,myObj2; // Create an object of
MyClass

myObj1.myNum = 15;
myObj1.myString = “XYZ";
class MyClass {
public: myObj2.myNum = 17;
int myNum; myObj2.myString = “ABC";
string myString;
}; cout << myObj1.myNum << "\n";
cout << myObj1.myString;

cout << myObj2.myNum << "\n";


cout << myObj2.myString;
return 0;
}
Create Class 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

• Inside Example

class MyClass {
public: int main() {
void myMethod() { MyClass myObj;
cout << "Hello myObj.myMethod();
World!"; return 0;
} }
};
Create Class Methods with Parameters

#include <iostream>
using namespace std;

class Car {
public:
int speed(int maxSpeed);
};

int Car::speed(int maxSpeed) {


return maxSpeed;
}

int main() {
Car myObj; // Create an object of Car
cout << myObj.speed(200); // Call the method with an argument
return 0;
}
C++ Access Specifiers

In C++, there are three access specifiers:


 public - members are accessible from outside the class
 private - members cannot be accessed (or viewed) from outside the class
 protected - members cannot be accessed from outside the class, however, they can
be accessed in inherited classes. You will learn more about Inheritance later.

class MyClass {
int main() {
public: // Public access
MyClass myObj;
specifier
myObj.x = 25; // Allowed (public)
int x; // Public attribute
myObj.y = 50; // Not allowed
private: // Private access
(private)
specifier
return 0;
int y; // Private attribute
}
};
C++ Access Specifiers

In C++, there are three access specifiers:


 public - members are accessible from outside the class
 private - members cannot be accessed (or viewed) from outside the class
 protected - members cannot be accessed from outside the class, however, they can
be accessed in inherited classes. You will learn more about Inheritance later.

class MyClass {
int x; // Private attribute
int y; // Private attribute
};

Note: By default, all members of a class are private if you don't


specify an access specifier
C++ Access Specifiers
class Shape {
public:
int main(void) {
void setWidth(int w) { Rectangle Rect;
width = w;}
void setHeight(int h) {
height = h;} Rect.setWidth(5);
protected:
Rect.setHeight(7);
int width;
int height;
};
// Print the area of the object.
// Derived class cout << "Total area: " << Rect.getArea() << endl;
class Rectangle: public Shape {
public:
int getArea() { return 0;
return (width * height);
}
}
};
C++ Constructors

• Constructor is a member function of a class.


• The name of the constructor is same as the name of the class.
• It has no return type, so can’t use return keyword.
• It must be an instance member function, that is, it can never be static.
• Constructor is implicitly invoked when an object created.
• Constructor is used to solve problem of initialization.

class MyClass { // The class


public: // Access int main() {
specifier MyClass myObj; // this will call the
MyClass() { // Constructor constructor
cout << "Hello World!"; return 0;
} }
};
C++ Parameterized Constructors
class Car { // The class
public: // Access specifier
string brand; string model; int year; // Attribute
Car(string x, string y, int z) { // Constructor with parameters
brand = x;
model = y;
year = z;
}
};
int main() {
// Create Car objects and call the constructor with different values
Car carObj1("BMW", "X5", 1999);
Car carObj2("Ford", "Mustang", 1969);

// Print values
cout << carObj1.brand << " " << carObj1.model << " " <<
carObj1.year << "\n";
cout << carObj2.brand << " " << carObj2.model << " " <<
carObj2.year << "\n";
return 0;
}
C++ Copy Constructors

class Complex{
private:
int a,b; int main()
public: {
Complex(int x,int y){ Complex
a=x;
C1(5,6),C2(10),C3(11,12);
b=y;
} Complex C4;
Void show() Complex C5=C1;
{ return 0;
cout<<" Constructor 2 Parameter "<<“\n a="<<a<<"b="<<b }
}
Complex(Complex &c){
a=c.a;
b=c.b;
}
};
C++ Destructor

• Destructor is an instance member function of a class


• The name of the destructor is same as the name of a class but preceded by tilde (~) symbol
• Destructor can never be static.
• Destructor has no return type.
• Destructor takes no argument (No overloading is possible)
• It is invoked implicitly when object is going to destroy.
class MyClass { // The class
public: // Access
specifier
int main() {
MyClass() { // Constructor
MyClass myObj1, MyObj2; // call the
cout << “I am in Constructor”
constructor
}
return 0;
~MyClass() { // Destructor
}
cout << “I am in Destructor”
}
};
C++ Friend Function

• Friend function is not a member function of a class to which it is friend.


• Friend function is declared in the class with friend keyword.
• It must be defined outside the class to which it is friend.
• Friend function can access any member of the class to which it is friend.
• Friend function cannot access members of the class directly.
• It has no caller object.
• It should not be defined with membership label.
C++ Friend Function
#include<iostream>
using namespace std;
class Complex{
private:
int a,b; void fun(Complex c)
public: {
void setData(int x,int y) cout<<"Sum is= "<<c.a+c.b;
{ }
a=x; int main()
b=y; {
} Complex c1,c2,c3;
void showData() c1.setData(9,10);
{ fun(c1);
cout<<"a="<<a<<"\n b="<<b;
} }
friend void fun(Complex c);
//Friend keyword is used to
declare //friend function
};
C++ Friend Class
• Similarly, like a friend function, a class can also be made a friend of another class using
keyword friend.
class A{ int main()
private: {
int a,b; A t1;
public: B t2;
void setData(int x,int y) t1.setData(9,10);
{ t2.showData(t1);
a=x; }
b=y;
} • When a class is made a friend class, all the member
friend class B; //Friend keyword
};
functions of that class becomes friend functions.
class B{ • In this program, all member functions of class B will be
public: friend functions of class A. Thus, any member function of
void showData(A t) class B can access the private and protected data of class
{ A. But, member functions of class A cannot access the data
cout<<"a="<<t.a<<"\n b="<<t.b; of class B.
} • Remember, friend relation in C++ is only granted, not
};
taken.
C++ Nested Classes in C++
• A nested class is a class that is declared in another class. The nested class is also a member variable of the enclosing
class and has the same access rights as the other members. However, the member functions of the enclosing class
have no special access to the members of a nested class.
int main() {
#include<iostream> cout<<"Nested classes in C++"<< endl;
using namespace std; A :: B obj;
class A { obj.getdata(9);
public: obj.putdata();
class B { return 0;
private: }
int num;
public: • In the above program, class B is defined inside the class A so it is
void getdata(int n) { a nested class. The class B contains a private variable num and
num = n;
two public functions getdata() and putdata(). The function
}
void putdata() { getdata() takes the data and the function putdata() displays the
cout<<"The number is "<<num; data.
} • In the function main(), an object of the class A and class B is
}; defined. Then the functions getdata() and putdata() are called
}; using the variable obj.
Function Overloading
With function overloading, multiple functions can have the same name with different
parameters:

int plusFuncInt(int x, int y) {


return x + y;
}

double plusFuncDouble(double x, double y) {


return x + y;
}

int main() {
int myNum1 = plusFuncInt(8, 5);
double myNum2 = plusFuncDouble(4.3, 6.26);
cout << "Int: " << myNum1 << "\n";
cout << "Double: " << myNum2;
return 0;
}
Function Overloading
• Instead of defining two functions that should do the same thing, it is better to overload one.
• In the example below, we overload the plusFunc function to work or both int and double:

int plusFunc(int x, int y) {


return x + y;
}

double plusFunc(double x, double y) {


return x + y;
}

int main() {
int myNum1 = plusFunc(8, 5);
double myNum2 = plusFunc(4.3, 6.26);
cout << "Int: " << myNum1 << "\n";
cout << "Double: " << myNum2;
return 0;
}
Encapsulation / Abstraction

Encapsulation in C++ is implemented as a class that bundles data and the functions operating on this
data together. Mostly data is declared as private so that it is not accessible outside the class. The methods or
functions are declared as public and can be accessed using the object of the class.

int main() {
class Employee { Employee myObj;
private: myObj.setSalary(50000);
// Private attribute cout << myObj.getSalary();
int salary; return 0;
}
public:
// Setter
void setSalary(int s) { Abstraction: The concept of abstraction only
salary = s; shows necessary information to the users. It
} reduces the complexity of the program by hiding
// Getter the implementation complexities of programs.
int getSalary() {
return salary;
}
};
Inheritance
In C++, it is possible to inherit attributes and methods from one class to another. We group the
"inheritance concept" into two categories:
• derived class (child) - the class that inherits from another class
• base class (parent) - the class being inherited from
To inherit from a class, use the : symbol.

// Base class
class Vehicle { int main() {
public: Car myCar;
string brand = “XYZ"; myCar.move();
void move() { cout << myCar.brand + " " + myCar.model;
cout << “car is moving \n" ; return 0;
} }
};

// Derived class
class Car: public Vehicle {
public:
string model = “ABC";
};
Polymorphism
Polymorphism means "many forms", and it occurs when we have many classes that are
related to each other by inheritance.
class Animal { // Base class
public: int main() {
void animalSound() { Animal myAnimal;
cout << "The animal makes a sound \ Pig myPig;
n" ; Dog myDog;
}
}; myAnimal.animalSound();
class Pig : public Animal { myPig.animalSound();
public: myDog.animalSound();
void animalSound() { return 0;
cout << "The pig says: wee wee \ }
n" ;
}
}; Why “Inheritance” and “Polymorphism”?
class Dog : public Animal {
- It is useful for code reusability: reuse attributes
public:
void animalSound() { and methods of an existing class when you create
cout << "The dog says: bow wow \ a new class.
n" ;
}
};
Function Overriding
Whenever we writing function in base and derived class in such a way that function name,
parameter must be same called function overriding.

Class a
{
public:
void display(){ cout << " hello"}
};

Class b:public a
{
public:
void display(){ cout << " hello"}
};
Function Overriding
It is the redefinition of base class function in its derived class with same signature i.e return type
and parameters. It can only be done in derived class.
Class A
{
public:
void fun(){ cout << “Ankit" <<endl; }
};

Class B:public a
{
public:
void fun(){ cout << “Ankit" <<endl; }
};
int main()
{
B obj;
obj.fun();
Obj.A::fun();
return 0;
}
Function Overloading Vs Function Overriding

Function Overloading Function Overriding


Inheritance: Overloading can occur without inheritance. Overriding of functions occurs when one class is
inherited from another class
Function Overloaded functions must differ in function In overriding, function signatures must be same.
Signature: signature ie. either number of parameters or
type of parameters should differ.
Scope of overloaded functions are in same scope Overridden functions are in different scopes;
functions:
Behaviour Overloading is used to have same name Overriding is needed when derived class function
of functions which behave differently depending has to do some added or different job than the
functions: upon parameters passed to them. base class function.
Structure
A structure is a collection of simple variables. The variables in a structure can be of different types: Some can
be int, some can be float, and so on. The data items in a structure are called the members of the structure
int main()
{
part part1; //define a structure variable
#include <iostream>
//values to str. Memb.
using namespace std;
part1.modelnumber = 6244;
struct part //declare a structure
part1.partnumber = 373;
{
part1.cost = 217.55F;
int modelnumber; //ID number of widget
//display str. Memb.
int partnumber; //ID number of widget part
cout << “Model “ << part1.modelnumber;
float cost; //cost of part
cout << “, part “ << part1.partnumber;
};
cout << “, costs $” << part1.cost << endl;
return 0;
}

The keyword struct introduces the structure definition. Next comes the structure name or tag, which is part. The
declarations of the structure members—model number, part number, and cost—are enclosed in braces. A semicolon
follows the closing brace, terminating the entire structure.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy