Open navigation menu
Close suggestions
Search
Search
en
Change Language
Upload
Sign in
Sign in
Download free for days
0 ratings
0% found this document useful (0 votes)
3 views
class and object in c++ in oops
Oops
Uploaded by
Dj Freefire
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
Download now
Download
Save class and object in c++ in oops For Later
Download
Save
Save class and object in c++ in oops For Later
0%
0% found this document useful, undefined
0%
, undefined
Embed
Share
Print
Report
0 ratings
0% found this document useful (0 votes)
3 views
class and object in c++ in oops
Oops
Uploaded by
Dj Freefire
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
Download now
Download
Save class and object in c++ in oops For Later
Carousel Previous
Carousel Next
Save
Save class and object in c++ in oops For Later
0%
0% found this document useful, undefined
0%
, undefined
Embed
Share
Print
Report
Download now
Download
You are on page 1
/ 9
Search
Fullscreen
36 +t C++Classesand Objects C++Polymorphism C++ Inheritance C++ Abstraction C++ Encapsulation C++ C++ Classes and Objects Last Updated : 15 Jul, 2024 In C++, classes and objects are the basic building block that leads to Object- Oriented programming in C++. In this article, we will learn about C++ classes, objects, look at how they work and how to implement them in our C++ program [a class is a user-defined data type, which holds its own data members and member functions, which can be accessed and used by creating an instance of that class. A C++ class is like a blueprint for an object. For Example: Consider the Class of Cars. There may be many cars with different names and brands but all of them will share some common properties like all of them will have 4 wheels, Speed Limit, Mileage range, etc. So here, the Car is the class, and wheels, speed limits, and mileage are their properties. vt A Class is a user-defined data type that has data members and member functions. Y Data members are the data variables and member functions are the functions used to manipulate these variables together, these data members and member functions define the properties and behaviour of the objects in a Class, \ In the above example of class Car, the data member will be speed limit, mileage, etc, and member functions can be applying brakes, increasing speed, etc. ~/ but we cannot use the class as itis. We first have to create an object of the class to use its features. An Object is an instance of a Class. Note: When a class is defined, no memory is allocated but when it isinstantiated (i.e. an object is created) memory is allocated. A class is defined in C++ using the keyword class followed by the name of the class. The following is the syntax: class ClassName { access_specifier: // Body of the class 4 Here, the access specifier defines the level of access to the class's data members. Example - class ThisClass { public: int var; // data menber void print() { // menber method cout << "Hello"; + 4 keyword user-defined name { Access specifier: H/can be private public or protected Data members; // Variables to be used Member Functions() {} //Methods to access data members h // Class name ends with a semicolonCwhen a class is defined, only the specification for the object is defined; no memory or storage is allocated. To use the data and access functions defined in the class, you need to create objects. Syntax to Create an Object ——_ We can create an object of the given class in the same way we declare the variables of any other inbuilt data type. ClassName ObjectName; Example MyClass obj; In the above statement, the object of MyClass with name obj is created. Accessing Data Members and Member Functions The data members and member functions of the class can be accessed using the dot(’’) operator with the object. For example, if the name of the object is obj and you want to access the member function with the name printName() then you will have to write: obj. printName() The below program shows how to define a simple class and how to create an object of it. cH a // C++ progran to iLtustrate how create a simple class and // object G — ¥include
include
> using namespace std; G 7 vefine a class named ‘Person‘ class Person {public // bata menbers string nane; int age; // Wenber 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 persont; // accessing data members personlsnane = "Alice"; personl,age = 303 // Call the introduce member method personl.introduce(); return 0; Output Hi, my name is Alice and I am 3@ years old. ————————————— In C++ classes, we can control the access to the members of the class using ‘Access Specifiers. Also known as access modifier, they are the keywords that are specified in the class and all the members of the class under that access specifier will have particular access level. In C++, there are 3 access specifiers that are as follows: 1, Public: Members declared as public can be accessed from outside the class. 2. Private: Members declared as private can only be accessed within the class itself. 3. Protected: Members declared as protected can be accessed within the class and by derived classes.If we do not specify the access specifier, the private specifier is applied to every member by default. Example of Access Specifiers CH o // C+ program to demonstrate accessing of data members Winclude
P using namespace std; class Geeks { D private: string geekrane; // Access specifier G pubtic: 71 Member Functions() void setNane(string name) { geeknane = name; } void printrane() { cout << "Geeknane is:" << geeknare; } % int main() { // declare an object of class geeks Geeks obj15 // accessing data menber // cannot do it Like: obj1.geekname = "Abhi"; obji.setNene("Abhi"); // accessing menber function cbj1.printname() ; return 0; } Output Geekname is:Abhi In the above example, we cannot access the data member geekname outside the class. If we try to access it in the main function using dot operator, obj1.geekname, then program will throw an error. There are 2 ways to define a member function: * Inside class definition * Outside class definitionTill now, we have defined the member function inside the class, but we can also define the member function outside the class. To define a member function outside the class definition, * We have to first declare the function prototype in the class definition. * Then we have to use the scope resolution:: operator along with the class name and function name. Example cH © // C++ program to demonstrate member function // definition outside class PF Hinclude
using namespace std; class Geeks { > public: string geekname; G int id; // printname is not defined inside class definition void printnane(); // printid is defined inside class definition void printid() { cout << "Geek id is: " << ids } // definition of printname using scope resolution operator cout << “Geekname is: " << geeknane; } int main() t Geeks ob91; obj. geeknare objid = 15; xy2"5 // call printname() obj. printname(); cout << endl; // call printid() obj1.printic(); return 0;Output Geekname is Geek id is: xyz 15 Note that all the member functions defined inside the class definition are by default inline, but you can also make any non-class function inline by using the keyword inline with them. Inline functions are actual functions, which are copied everywhere during compilation, like pre-processor macro, so the overhead of function calls is reduced. Note: Declaring a friend function is a way to give private access to a non- member function. Constructors Constructors are special class members which are called by the compiler every time an object of that class is instantiated. Constructors have the same name as the class and may be defined inside or outside the class definition. There are 4 types of constructors in C++ classes: * Default Constructors: The constructor that takes no argument is called default constructor. * Parameterized Constructors: This type of constructor takes the arguments to initialize the data members. * Copy Constructors: Copy constructor creates the object from an already existing object by copying it. * Move Constructor: The move constructor also creates the object from an already existing object but by moving it. Example of Constructor cH// C++ program to demonstrate constructors include
using namespace std; class Geeks { public: int id; evrxa@A //oefault Constructor Geeks() { cout << "Default Constructor called” << endl; id=-15 + //Paraneterized Constructor Geeks(int x) { cout <<"Parameterized Constructor called "<< endl; id-x; } ub int main() { // objt will call Default Constructor Geeks obj1; cout <<"Geek id is: "ccobj1.id << endl; // 0bj2 will call Parameterized Constructor Geeks 0b52(21); cout <<"Geek id is: " <
using namespace std; class Geeks { ‘ public: int ids //oefinition for Destructor ~Geeks() { } cout << "Destructor called for id << id
You might also like
Unit 5 Notes
PDF
No ratings yet
Unit 5 Notes
122 pages
2 C++
PDF
No ratings yet
2 C++
137 pages
unit-3.1
PDF
No ratings yet
unit-3.1
41 pages
05 Object Oriented Concept
PDF
No ratings yet
05 Object Oriented Concept
30 pages
class & obj gfg
PDF
No ratings yet
class & obj gfg
10 pages
Uni T-I:Arrays: Ani NT R Oduct I Ont Oc++Cl Ass: Abst R Act I On
PDF
No ratings yet
Uni T-I:Arrays: Ani NT R Oduct I Ont Oc++Cl Ass: Abst R Act I On
15 pages
Unit 2
PDF
No ratings yet
Unit 2
36 pages
Class & Class Members C++
PDF
No ratings yet
Class & Class Members C++
10 pages
C++ Classes and Objects
PDF
No ratings yet
C++ Classes and Objects
4 pages
C++ Object and Class
PDF
No ratings yet
C++ Object and Class
35 pages
Unit 2 - OOP
PDF
No ratings yet
Unit 2 - OOP
22 pages
Unit 5 Classesnobjects
PDF
No ratings yet
Unit 5 Classesnobjects
71 pages
05 Object Oriented Concept
PDF
No ratings yet
05 Object Oriented Concept
30 pages
3rd Chapter
PDF
No ratings yet
3rd Chapter
12 pages
L3 Classes
PDF
No ratings yet
L3 Classes
35 pages
Classes Oops CPP
PDF
No ratings yet
Classes Oops CPP
2 pages
Lecture2
PDF
No ratings yet
Lecture2
42 pages
Lecture 6_ Object Oriented Programming
PDF
No ratings yet
Lecture 6_ Object Oriented Programming
72 pages
CPP Notes
PDF
No ratings yet
CPP Notes
31 pages
OOPS - Durvesh Jagtap - C++
PDF
No ratings yet
OOPS - Durvesh Jagtap - C++
22 pages
AV223 Lab01 Classes&Objects
PDF
No ratings yet
AV223 Lab01 Classes&Objects
5 pages
OOSD UNIT 5
PDF
No ratings yet
OOSD UNIT 5
27 pages
Unit-5 PPT Notes
PDF
No ratings yet
Unit-5 PPT Notes
48 pages
Week4a_Classes and Objects
PDF
No ratings yet
Week4a_Classes and Objects
22 pages
3-Lecture-DSOOP-Classes-DrTahirNawaz
PDF
No ratings yet
3-Lecture-DSOOP-Classes-DrTahirNawaz
29 pages
C++ Oops Concepts
PDF
No ratings yet
C++ Oops Concepts
10 pages
Constructor, destructors and inheritance
PDF
No ratings yet
Constructor, destructors and inheritance
29 pages
Industrial Training File
PDF
No ratings yet
Industrial Training File
29 pages
Unit 3 Classes and Objects
PDF
No ratings yet
Unit 3 Classes and Objects
40 pages
Class and Object UNIT-4: by Aditya Bhardwaj
PDF
No ratings yet
Class and Object UNIT-4: by Aditya Bhardwaj
50 pages
CPP Classes Objects
PDF
No ratings yet
CPP Classes Objects
3 pages
Chapter 08
PDF
No ratings yet
Chapter 08
45 pages
M2 oops
PDF
No ratings yet
M2 oops
135 pages
Unit II Classes and Objects, Constructors and Destructors-1
PDF
No ratings yet
Unit II Classes and Objects, Constructors and Destructors-1
86 pages
Lab 02 - Oop
PDF
No ratings yet
Lab 02 - Oop
7 pages
OOPc++ standard
PDF
No ratings yet
OOPc++ standard
21 pages
EC-204 Data Structures and Object Oriented Programming: Classes
PDF
No ratings yet
EC-204 Data Structures and Object Oriented Programming: Classes
28 pages
Oops
PDF
No ratings yet
Oops
16 pages
IT 304 OOPM Unit II - 1693892198
PDF
No ratings yet
IT 304 OOPM Unit II - 1693892198
14 pages
Class Objects
PDF
No ratings yet
Class Objects
4 pages
Object Oriented Programming (OOP) in C++
PDF
No ratings yet
Object Oriented Programming (OOP) in C++
13 pages
6 C++ Classes and Objects - Handout 6 PDF
PDF
No ratings yet
6 C++ Classes and Objects - Handout 6 PDF
15 pages
Chapter 5 OOP Using C++
PDF
No ratings yet
Chapter 5 OOP Using C++
15 pages
DAY 2 Class MemberFunction Constructor
PDF
No ratings yet
DAY 2 Class MemberFunction Constructor
35 pages
CS205-2020 Spring - Lecture 9 PDF
PDF
No ratings yet
CS205-2020 Spring - Lecture 9 PDF
27 pages
C++ 1
PDF
No ratings yet
C++ 1
7 pages
C++ - CLASSES
PDF
No ratings yet
C++ - CLASSES
25 pages
Chapter 1 CPP
PDF
No ratings yet
Chapter 1 CPP
23 pages
c++theory
PDF
No ratings yet
c++theory
38 pages
C++ Classes and Objects
PDF
No ratings yet
C++ Classes and Objects
4 pages
C++ CLASSES AND OBJECTS, Constructors Update
PDF
No ratings yet
C++ CLASSES AND OBJECTS, Constructors Update
25 pages
OOPs With C++ Notes
PDF
No ratings yet
OOPs With C++ Notes
153 pages
unit 3 205
PDF
No ratings yet
unit 3 205
47 pages
12 Classes and Objects
PDF
No ratings yet
12 Classes and Objects
22 pages
Lecture-3-Objects and Classes Concept
PDF
No ratings yet
Lecture-3-Objects and Classes Concept
22 pages
Lesson 6 Classes: Objectives
PDF
No ratings yet
Lesson 6 Classes: Objectives
14 pages
03CSE225CPlusPlus Part01
PDF
No ratings yet
03CSE225CPlusPlus Part01
35 pages
ITE 6102 - Computer Programming 1 - VC - Sept 11
PDF
No ratings yet
ITE 6102 - Computer Programming 1 - VC - Sept 11
49 pages
Related titles
Click to expand Related Titles
Carousel Previous
Carousel Next
Unit 5 Notes
PDF
Unit 5 Notes
2 C++
PDF
2 C++
unit-3.1
PDF
unit-3.1
05 Object Oriented Concept
PDF
05 Object Oriented Concept
class & obj gfg
PDF
class & obj gfg
Uni T-I:Arrays: Ani NT R Oduct I Ont Oc++Cl Ass: Abst R Act I On
PDF
Uni T-I:Arrays: Ani NT R Oduct I Ont Oc++Cl Ass: Abst R Act I On
Unit 2
PDF
Unit 2
Class & Class Members C++
PDF
Class & Class Members C++
C++ Classes and Objects
PDF
C++ Classes and Objects
C++ Object and Class
PDF
C++ Object and Class
Unit 2 - OOP
PDF
Unit 2 - OOP
Unit 5 Classesnobjects
PDF
Unit 5 Classesnobjects
05 Object Oriented Concept
PDF
05 Object Oriented Concept
3rd Chapter
PDF
3rd Chapter
L3 Classes
PDF
L3 Classes
Classes Oops CPP
PDF
Classes Oops CPP
Lecture2
PDF
Lecture2
Lecture 6_ Object Oriented Programming
PDF
Lecture 6_ Object Oriented Programming
CPP Notes
PDF
CPP Notes
OOPS - Durvesh Jagtap - C++
PDF
OOPS - Durvesh Jagtap - C++
AV223 Lab01 Classes&Objects
PDF
AV223 Lab01 Classes&Objects
OOSD UNIT 5
PDF
OOSD UNIT 5
Unit-5 PPT Notes
PDF
Unit-5 PPT Notes
Week4a_Classes and Objects
PDF
Week4a_Classes and Objects
3-Lecture-DSOOP-Classes-DrTahirNawaz
PDF
3-Lecture-DSOOP-Classes-DrTahirNawaz
C++ Oops Concepts
PDF
C++ Oops Concepts
Constructor, destructors and inheritance
PDF
Constructor, destructors and inheritance
Industrial Training File
PDF
Industrial Training File
Unit 3 Classes and Objects
PDF
Unit 3 Classes and Objects
Class and Object UNIT-4: by Aditya Bhardwaj
PDF
Class and Object UNIT-4: by Aditya Bhardwaj
CPP Classes Objects
PDF
CPP Classes Objects
Chapter 08
PDF
Chapter 08
M2 oops
PDF
M2 oops
Unit II Classes and Objects, Constructors and Destructors-1
PDF
Unit II Classes and Objects, Constructors and Destructors-1
Lab 02 - Oop
PDF
Lab 02 - Oop
OOPc++ standard
PDF
OOPc++ standard
EC-204 Data Structures and Object Oriented Programming: Classes
PDF
EC-204 Data Structures and Object Oriented Programming: Classes
Oops
PDF
Oops
IT 304 OOPM Unit II - 1693892198
PDF
IT 304 OOPM Unit II - 1693892198
Class Objects
PDF
Class Objects
Object Oriented Programming (OOP) in C++
PDF
Object Oriented Programming (OOP) in C++
6 C++ Classes and Objects - Handout 6 PDF
PDF
6 C++ Classes and Objects - Handout 6 PDF
Chapter 5 OOP Using C++
PDF
Chapter 5 OOP Using C++
DAY 2 Class MemberFunction Constructor
PDF
DAY 2 Class MemberFunction Constructor
CS205-2020 Spring - Lecture 9 PDF
PDF
CS205-2020 Spring - Lecture 9 PDF
C++ 1
PDF
C++ 1
C++ - CLASSES
PDF
C++ - CLASSES
Chapter 1 CPP
PDF
Chapter 1 CPP
c++theory
PDF
c++theory
C++ Classes and Objects
PDF
C++ Classes and Objects
C++ CLASSES AND OBJECTS, Constructors Update
PDF
C++ CLASSES AND OBJECTS, Constructors Update
OOPs With C++ Notes
PDF
OOPs With C++ Notes
unit 3 205
PDF
unit 3 205
12 Classes and Objects
PDF
12 Classes and Objects
Lecture-3-Objects and Classes Concept
PDF
Lecture-3-Objects and Classes Concept
Lesson 6 Classes: Objectives
PDF
Lesson 6 Classes: Objectives
03CSE225CPlusPlus Part01
PDF
03CSE225CPlusPlus Part01
ITE 6102 - Computer Programming 1 - VC - Sept 11
PDF
ITE 6102 - Computer Programming 1 - VC - Sept 11