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
The Subtle Art of Not Giving a F*ck: A Counterintuitive Approach to Living a Good Life
From Everand
The Subtle Art of Not Giving a F*ck: A Counterintuitive Approach to Living a Good Life
Mark Manson
4/5 (6129)
Principles: Life and Work
From Everand
Principles: Life and Work
Ray Dalio
4/5 (627)
The Gifts of Imperfection: Let Go of Who You Think You're Supposed to Be and Embrace Who You Are
From Everand
The Gifts of Imperfection: Let Go of Who You Think You're Supposed to Be and Embrace Who You Are
Brene Brown
4/5 (1148)
Never Split the Difference: Negotiating As If Your Life Depended On It
From Everand
Never Split the Difference: Negotiating As If Your Life Depended On It
Chris Voss
4.5/5 (935)
The Glass Castle: A Memoir
From Everand
The Glass Castle: A Memoir
Jeannette Walls
4/5 (8215)
Grit: The Power of Passion and Perseverance
From Everand
Grit: The Power of Passion and Perseverance
Angela Duckworth
4/5 (631)
Sing, Unburied, Sing: A Novel
From Everand
Sing, Unburied, Sing: A Novel
Jesmyn Ward
4/5 (1253)
The Perks of Being a Wallflower
From Everand
The Perks of Being a Wallflower
Stephen Chbosky
4/5 (8365)
Shoe Dog: A Memoir by the Creator of Nike
From Everand
Shoe Dog: A Memoir by the Creator of Nike
Phil Knight
4.5/5 (860)
Her Body and Other Parties: Stories
From Everand
Her Body and Other Parties: Stories
Carmen Maria Machado
4/5 (877)
The Hard Thing About Hard Things: Building a Business When There Are No Easy Answers
From Everand
The Hard Thing About Hard Things: Building a Business When There Are No Easy Answers
Ben Horowitz
4.5/5 (361)
Hidden Figures: The American Dream and the Untold Story of the Black Women Mathematicians Who Helped Win the Space Race
From Everand
Hidden Figures: The American Dream and the Untold Story of the Black Women Mathematicians Who Helped Win the Space Race
Margot Lee Shetterly
4/5 (954)
Steve Jobs
From Everand
Steve Jobs
Walter Isaacson
4/5 (2923)
Elon Musk: Tesla, SpaceX, and the Quest for a Fantastic Future
From Everand
Elon Musk: Tesla, SpaceX, and the Quest for a Fantastic Future
Ashlee Vance
4.5/5 (484)
The Emperor of All Maladies: A Biography of Cancer
From Everand
The Emperor of All Maladies: A Biography of Cancer
Siddhartha Mukherjee
4.5/5 (277)
A Man Called Ove: A Novel
From Everand
A Man Called Ove: A Novel
Fredrik Backman
4.5/5 (4972)
Angela's Ashes: A Memoir
From Everand
Angela's Ashes: A Memoir
Frank McCourt
4.5/5 (444)
Brooklyn: A Novel
From Everand
Brooklyn: A Novel
Colm Toibin
3.5/5 (2061)
The Art of Racing in the Rain: A Novel
From Everand
The Art of Racing in the Rain: A Novel
Garth Stein
4/5 (4281)
The Yellow House: A Memoir (2019 National Book Award Winner)
From Everand
The Yellow House: A Memoir (2019 National Book Award Winner)
Sarah M. Broom
4/5 (100)
The Little Book of Hygge: Danish Secrets to Happy Living
From Everand
The Little Book of Hygge: Danish Secrets to Happy Living
Meik Wiking
3.5/5 (447)
The World Is Flat 3.0: A Brief History of the Twenty-first Century
From Everand
The World Is Flat 3.0: A Brief History of the Twenty-first Century
Thomas L. Friedman
3.5/5 (2283)
Devil in the Grove: Thurgood Marshall, the Groveland Boys, and the Dawn of a New America
From Everand
Devil in the Grove: Thurgood Marshall, the Groveland Boys, and the Dawn of a New America
Gilbert King
4.5/5 (278)
Yes Please
From Everand
Yes Please
Amy Poehler
4/5 (1987)
Bad Feminist: Essays
From Everand
Bad Feminist: Essays
Roxane Gay
4/5 (1068)
The Outsider: A Novel
From Everand
The Outsider: A Novel
Stephen King
4/5 (1993)
The Woman in Cabin 10
From Everand
The Woman in Cabin 10
Ruth Ware
3.5/5 (2641)
A Tree Grows in Brooklyn
From Everand
A Tree Grows in Brooklyn
Betty Smith
4.5/5 (1936)
The Sympathizer: A Novel (Pulitzer Prize for Fiction)
From Everand
The Sympathizer: A Novel (Pulitzer Prize for Fiction)
Viet Thanh Nguyen
4.5/5 (125)
A Heartbreaking Work Of Staggering Genius: A Memoir Based on a True Story
From Everand
A Heartbreaking Work Of Staggering Genius: A Memoir Based on a True Story
Dave Eggers
3.5/5 (692)
Team of Rivals: The Political Genius of Abraham Lincoln
From Everand
Team of Rivals: The Political Genius of Abraham Lincoln
Doris Kearns Goodwin
4.5/5 (1912)
Wolf Hall: A Novel
From Everand
Wolf Hall: A Novel
Hilary Mantel
4/5 (4074)
On Fire: The (Burning) Case for a Green New Deal
From Everand
On Fire: The (Burning) Case for a Green New Deal
Naomi Klein
4/5 (75)
Fear: Trump in the White House
From Everand
Fear: Trump in the White House
Bob Woodward
3.5/5 (830)
Manhattan Beach: A Novel
From Everand
Manhattan Beach: A Novel
Jennifer Egan
3.5/5 (901)
Rise of ISIS: A Threat We Can't Ignore
From Everand
Rise of ISIS: A Threat We Can't Ignore
Jay Sekulow
3.5/5 (143)
John Adams
From Everand
John Adams
David McCullough
4.5/5 (2544)
The Light Between Oceans: A Novel
From Everand
The Light Between Oceans: A Novel
M L Stedman
4.5/5 (790)
The Unwinding: An Inner History of the New America
From Everand
The Unwinding: An Inner History of the New America
George Packer
4/5 (45)
Little Women
From Everand
Little Women
Louisa May Alcott
4/5 (105)
The Constant Gardener: A Novel
From Everand
The Constant Gardener: A Novel
John le Carré
3.5/5 (109)