1. The document discusses various concepts related to functions in C++ such as function prototypes, passing arguments by reference, default arguments, inline functions, function overloading, and friend functions.
2. It provides examples to explain concepts like passing arguments by reference allows altering the original variable values, a friend function can access private members of a class, and function overloading allows using the same function name for different tasks based on the argument types.
3. The key benefits of concepts like inline functions, passing by reference, and function overloading are also summarized.
Multidimensional arrays store data in tabular form with multiple indices. A 3D array declaration would be datatype arrayName[size1][size2][size3]. Elements can be initialized and accessed similar to 2D arrays but with additional nested brackets and loops for each dimension. Functions allow dividing a problem into smaller logical parts. Functions are defined with a return type, name, parameters and body. Arguments are passed by value or reference and arrays can also be passed to functions. Recursion occurs when a function calls itself, requiring a base case to terminate the recursion.
1. Inline functions are small functions whose code is inserted at the call site instead of generating a function call. This avoids overhead of function calls but increases code size.
2. Function overloading allows different functions to have the same name but different parameters. The compiler determines which version to call based on argument types.
3. C++ classes allow defining data types that bundle together data members and member functions that can access them. Classes support data hiding and inheritance.
6. Functions in C ++ programming object oriented programmingAhmad177077
In C++, functions are used to organize code into modular blocks that can perform specific tasks. Functions allow you to avoid code repetition, improve code readability, and make your program more manageable.
This document discusses different types of functions in C++ including: library functions, user-defined functions, overloaded functions, inline functions, friend functions, static functions, constructor functions, destructor functions, and virtual functions. It provides examples and definitions for each type of function.
The document discusses various concepts related to functions and operator overloading in C++, including:
1. It describes how functions can be divided into smaller modules to more easily design, build, debug, extend, modify, understand, reuse, and organize large programs.
2. It explains that C++ supports defining multiple functions with the same name but different argument lists through function overloading.
3. It provides examples of overloading operators like +, -, <, <=, assignment (=), increment (++), and decrement (--) operators for user-defined classes.
This C++ PowerPoint presentation stands out for its clear and concise explanation of complex concepts, making it accessible to both beginners and advanced programmers. The structured flow from basic syntax to more advanced topics like object-oriented programming and memory management showcases a deep understanding of the language. The visuals, including diagrams and code snippets, enhance comprehension and keep the audience engaged. Overall, the presentation strikes a perfect balance between technical depth and visual clarity, making it an excellent resource for anyone learning or teaching C++.
Chapter 6 - Modular Programming- in C++.pptxChereLemma2
Here is a function that meets the requirements:
int getProduct(int num1, double num2) {
return num1 * num2;
}
This function:
- Is named getProduct
- Has two parameters: num1 which is an int, and num2 which is a double
- Returns the product of num1 and num2 by multiplying them together and returning the result
- Complies with the data types and operation specified in the requirements
The document provides information on functions in C++. It defines a function as a self-contained block of code that performs a specific task. The key points made include:
1. Functions have a name, return type, arguments, and body of code. They may be library functions or user-defined.
2. Functions are declared with a prototype and defined with a body. They are called by passing arguments.
3. Functions return a value using the return statement. Default return type is int.
4. Functions can have default arguments, be inline to reduce overhead, or be overloaded based on parameters. Recursion and passing objects are also discussed.
This document provides an overview of the C++ programming language. It discusses key C++ concepts like classes, objects, functions, and data types. Some key points:
- C++ is an object-oriented language that is an extension of C with additional features like classes and inheritance.
- Classes allow programmers to combine data and functions to model real-world entities. Objects are instances of classes.
- The document defines common C++ terms like keywords, identifiers, constants, and operators. It also provides examples of basic programs.
- Functions are described as modular and reusable blocks of code. Parameter passing techniques like pass-by-value and pass-by-reference are covered.
- Other concepts covered include
This document discusses different types of functions in C++, including user-defined functions, library functions, function parameters, return values, function prototypes, and function overloading. It provides examples to illustrate key concepts like defining functions with different parameters and return types, passing arguments to functions, and returning values from functions. Storage classes like local, global, static local and register variables are also briefly covered. The document is an introduction to functions in C++ programming.
The document discusses different types of storage classes in C++ that determine the lifetime and scope of variables:
1. Local variables are defined inside functions and have scope limited to that function. They are destroyed when the function exits.
2. Global variables are defined outside all functions and have scope in the entire program. They are destroyed when the program ends.
3. Static local variables are local variables that retain their value between function calls. Register variables are local variables stored in processor registers for faster access.
4. Thread local storage allows defining variables that are local to each thread and retain their values similar to static variables. The document provides examples to illustrate local, global, and static variables.
The document discusses classes and objects in C++. It begins with an overview of structures and how classes are similar but allow for data abstraction and hiding implementation details. It then discusses the key differences between classes and structures, such as members being private by default in classes versus public in structures. The document also covers defining classes with data members and member functions, creating objects of a class, accessing class members, defining member functions inside and outside classes, and other concepts like static members, arrays within classes, and passing objects as function arguments.
The document discusses functions in C programming. It defines functions as mini-programs that can take in inputs, execute statements, and return outputs. Functions allow programmers to break large tasks into smaller, reusable parts. The key aspects of functions covered include: defining functions with return types and parameters; calling functions and passing arguments; return values; function prototypes; recursion; and examples of calculating factorials and acceleration using functions.
- Classes and objects allow programmers to bundle data and functions that work on that data together. A class defines the data and functions, while an object is an instance of a class.
- The main differences between classes and structures in C++ are that class members are private by default while structure members are public, and objects of classes can allow null values while objects of structures cannot.
- Constructors are special member functions that are called automatically when an object is created. They are used to initialize the data members of newly created objects. Constructors have the same name as the class but do not have a return type.
Functions in C++ provide more capabilities than functions in C, including function prototypes, function overloading, default arguments, and operator overloading. Function prototypes allow strong type checking and prevent illegal values from being passed to functions. Function overloading allows multiple functions to have the same name if they have different parameters. Default arguments allow functions to be called without passing all parameters by using default values defined in the function prototype. Operator overloading extends operators like + and - to non-standard data types like strings. Inline functions help reduce function call overhead by inserting the function code directly in the calling code.
Functions allow programmers to structure C++ programs into modular segments of code to perform individual tasks. There are two types of functions: library functions and user-defined functions. User-defined functions are defined using a return type, function name, and parameters. Functions can be called by value or by reference and can also be inline, recursive, or friend functions.
This document discusses functions in C language. It defines what a function is, its properties, types of functions, and how to define, declare and call functions. The key points are:
1. A function is a block of code that performs a specific task and can be called from different parts of a program.
2. Functions have a unique name, are independent units that perform tasks without interfering with other code, and can optionally return a value.
3. Functions are declared with a prototype specifying their return type, name and parameters, and defined with the actual code implementation.
C++ functions require prototypes that specify the return type and parameters. Function overloading allows multiple functions to have the same name but different signatures. Default arguments allow functions to be called without providing trailing arguments. Inline functions expand the function body at the call site for small functions to reduce overhead compared to regular function calls.
The document discusses functions in C programming. It defines a function as a block of code that performs a specific task. There are two types of functions: predefined standard library functions and user-defined functions. The key aspects of a function are its declaration, definition, and call. Functions can be used to break a large program into smaller, reusable components. Parameters can be passed to functions by value or by reference. Recursion is when a function calls itself, and is used in algorithms like calculating factorials. Dynamic memory allocation allows programs to request memory at runtime using functions like malloc(), calloc(), realloc(), and free().
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungenpanagenda
Webinar Recording: https://www.panagenda.com/webinars/hcl-nomad-web-best-practices-und-verwaltung-von-multiuser-umgebungen/
HCL Nomad Web wird als die nächste Generation des HCL Notes-Clients gefeiert und bietet zahlreiche Vorteile, wie die Beseitigung des Bedarfs an Paketierung, Verteilung und Installation. Nomad Web-Client-Updates werden “automatisch” im Hintergrund installiert, was den administrativen Aufwand im Vergleich zu traditionellen HCL Notes-Clients erheblich reduziert. Allerdings stellt die Fehlerbehebung in Nomad Web im Vergleich zum Notes-Client einzigartige Herausforderungen dar.
Begleiten Sie Christoph und Marc, während sie demonstrieren, wie der Fehlerbehebungsprozess in HCL Nomad Web vereinfacht werden kann, um eine reibungslose und effiziente Benutzererfahrung zu gewährleisten.
In diesem Webinar werden wir effektive Strategien zur Diagnose und Lösung häufiger Probleme in HCL Nomad Web untersuchen, einschließlich
- Zugriff auf die Konsole
- Auffinden und Interpretieren von Protokolldateien
- Zugriff auf den Datenordner im Cache des Browsers (unter Verwendung von OPFS)
- Verständnis der Unterschiede zwischen Einzel- und Mehrbenutzerszenarien
- Nutzung der Client Clocking-Funktion
Ad
More Related Content
Similar to Object Oriented Programming using C++ Unit 1 (20)
This C++ PowerPoint presentation stands out for its clear and concise explanation of complex concepts, making it accessible to both beginners and advanced programmers. The structured flow from basic syntax to more advanced topics like object-oriented programming and memory management showcases a deep understanding of the language. The visuals, including diagrams and code snippets, enhance comprehension and keep the audience engaged. Overall, the presentation strikes a perfect balance between technical depth and visual clarity, making it an excellent resource for anyone learning or teaching C++.
Chapter 6 - Modular Programming- in C++.pptxChereLemma2
Here is a function that meets the requirements:
int getProduct(int num1, double num2) {
return num1 * num2;
}
This function:
- Is named getProduct
- Has two parameters: num1 which is an int, and num2 which is a double
- Returns the product of num1 and num2 by multiplying them together and returning the result
- Complies with the data types and operation specified in the requirements
The document provides information on functions in C++. It defines a function as a self-contained block of code that performs a specific task. The key points made include:
1. Functions have a name, return type, arguments, and body of code. They may be library functions or user-defined.
2. Functions are declared with a prototype and defined with a body. They are called by passing arguments.
3. Functions return a value using the return statement. Default return type is int.
4. Functions can have default arguments, be inline to reduce overhead, or be overloaded based on parameters. Recursion and passing objects are also discussed.
This document provides an overview of the C++ programming language. It discusses key C++ concepts like classes, objects, functions, and data types. Some key points:
- C++ is an object-oriented language that is an extension of C with additional features like classes and inheritance.
- Classes allow programmers to combine data and functions to model real-world entities. Objects are instances of classes.
- The document defines common C++ terms like keywords, identifiers, constants, and operators. It also provides examples of basic programs.
- Functions are described as modular and reusable blocks of code. Parameter passing techniques like pass-by-value and pass-by-reference are covered.
- Other concepts covered include
This document discusses different types of functions in C++, including user-defined functions, library functions, function parameters, return values, function prototypes, and function overloading. It provides examples to illustrate key concepts like defining functions with different parameters and return types, passing arguments to functions, and returning values from functions. Storage classes like local, global, static local and register variables are also briefly covered. The document is an introduction to functions in C++ programming.
The document discusses different types of storage classes in C++ that determine the lifetime and scope of variables:
1. Local variables are defined inside functions and have scope limited to that function. They are destroyed when the function exits.
2. Global variables are defined outside all functions and have scope in the entire program. They are destroyed when the program ends.
3. Static local variables are local variables that retain their value between function calls. Register variables are local variables stored in processor registers for faster access.
4. Thread local storage allows defining variables that are local to each thread and retain their values similar to static variables. The document provides examples to illustrate local, global, and static variables.
The document discusses classes and objects in C++. It begins with an overview of structures and how classes are similar but allow for data abstraction and hiding implementation details. It then discusses the key differences between classes and structures, such as members being private by default in classes versus public in structures. The document also covers defining classes with data members and member functions, creating objects of a class, accessing class members, defining member functions inside and outside classes, and other concepts like static members, arrays within classes, and passing objects as function arguments.
The document discusses functions in C programming. It defines functions as mini-programs that can take in inputs, execute statements, and return outputs. Functions allow programmers to break large tasks into smaller, reusable parts. The key aspects of functions covered include: defining functions with return types and parameters; calling functions and passing arguments; return values; function prototypes; recursion; and examples of calculating factorials and acceleration using functions.
- Classes and objects allow programmers to bundle data and functions that work on that data together. A class defines the data and functions, while an object is an instance of a class.
- The main differences between classes and structures in C++ are that class members are private by default while structure members are public, and objects of classes can allow null values while objects of structures cannot.
- Constructors are special member functions that are called automatically when an object is created. They are used to initialize the data members of newly created objects. Constructors have the same name as the class but do not have a return type.
Functions in C++ provide more capabilities than functions in C, including function prototypes, function overloading, default arguments, and operator overloading. Function prototypes allow strong type checking and prevent illegal values from being passed to functions. Function overloading allows multiple functions to have the same name if they have different parameters. Default arguments allow functions to be called without passing all parameters by using default values defined in the function prototype. Operator overloading extends operators like + and - to non-standard data types like strings. Inline functions help reduce function call overhead by inserting the function code directly in the calling code.
Functions allow programmers to structure C++ programs into modular segments of code to perform individual tasks. There are two types of functions: library functions and user-defined functions. User-defined functions are defined using a return type, function name, and parameters. Functions can be called by value or by reference and can also be inline, recursive, or friend functions.
This document discusses functions in C language. It defines what a function is, its properties, types of functions, and how to define, declare and call functions. The key points are:
1. A function is a block of code that performs a specific task and can be called from different parts of a program.
2. Functions have a unique name, are independent units that perform tasks without interfering with other code, and can optionally return a value.
3. Functions are declared with a prototype specifying their return type, name and parameters, and defined with the actual code implementation.
C++ functions require prototypes that specify the return type and parameters. Function overloading allows multiple functions to have the same name but different signatures. Default arguments allow functions to be called without providing trailing arguments. Inline functions expand the function body at the call site for small functions to reduce overhead compared to regular function calls.
The document discusses functions in C programming. It defines a function as a block of code that performs a specific task. There are two types of functions: predefined standard library functions and user-defined functions. The key aspects of a function are its declaration, definition, and call. Functions can be used to break a large program into smaller, reusable components. Parameters can be passed to functions by value or by reference. Recursion is when a function calls itself, and is used in algorithms like calculating factorials. Dynamic memory allocation allows programs to request memory at runtime using functions like malloc(), calloc(), realloc(), and free().
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungenpanagenda
Webinar Recording: https://www.panagenda.com/webinars/hcl-nomad-web-best-practices-und-verwaltung-von-multiuser-umgebungen/
HCL Nomad Web wird als die nächste Generation des HCL Notes-Clients gefeiert und bietet zahlreiche Vorteile, wie die Beseitigung des Bedarfs an Paketierung, Verteilung und Installation. Nomad Web-Client-Updates werden “automatisch” im Hintergrund installiert, was den administrativen Aufwand im Vergleich zu traditionellen HCL Notes-Clients erheblich reduziert. Allerdings stellt die Fehlerbehebung in Nomad Web im Vergleich zum Notes-Client einzigartige Herausforderungen dar.
Begleiten Sie Christoph und Marc, während sie demonstrieren, wie der Fehlerbehebungsprozess in HCL Nomad Web vereinfacht werden kann, um eine reibungslose und effiziente Benutzererfahrung zu gewährleisten.
In diesem Webinar werden wir effektive Strategien zur Diagnose und Lösung häufiger Probleme in HCL Nomad Web untersuchen, einschließlich
- Zugriff auf die Konsole
- Auffinden und Interpretieren von Protokolldateien
- Zugriff auf den Datenordner im Cache des Browsers (unter Verwendung von OPFS)
- Verständnis der Unterschiede zwischen Einzel- und Mehrbenutzerszenarien
- Nutzung der Client Clocking-Funktion
Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...Aqusag Technologies
In late April 2025, a significant portion of Europe, particularly Spain, Portugal, and parts of southern France, experienced widespread, rolling power outages that continue to affect millions of residents, businesses, and infrastructure systems.
Procurement Insights Cost To Value Guide.pptxJon Hansen
Procurement Insights integrated Historic Procurement Industry Archives, serves as a powerful complement — not a competitor — to other procurement industry firms. It fills critical gaps in depth, agility, and contextual insight that most traditional analyst and association models overlook.
Learn more about this value- driven proprietary service offering here.
IT help desk outsourcing Services can assist with that by offering availability for customers and address their IT issue promptly without breaking the bank.
Spark is a powerhouse for large datasets, but when it comes to smaller data workloads, its overhead can sometimes slow things down. What if you could achieve high performance and efficiency without the need for Spark?
At S&P Global Commodity Insights, having a complete view of global energy and commodities markets enables customers to make data-driven decisions with confidence and create long-term, sustainable value. 🌍
Explore delta-rs + CDC and how these open-source innovations power lightweight, high-performance data applications beyond Spark! 🚀
This is the keynote of the Into the Box conference, highlighting the release of the BoxLang JVM language, its key enhancements, and its vision for the future.
Special Meetup Edition - TDX Bengaluru Meetup #52.pptxshyamraj55
We’re bringing the TDX energy to our community with 2 power-packed sessions:
🛠️ Workshop: MuleSoft for Agentforce
Explore the new version of our hands-on workshop featuring the latest Topic Center and API Catalog updates.
📄 Talk: Power Up Document Processing
Dive into smart automation with MuleSoft IDP, NLP, and Einstein AI for intelligent document workflows.
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025BookNet Canada
Book industry standards are evolving rapidly. In the first part of this session, we’ll share an overview of key developments from 2024 and the early months of 2025. Then, BookNet’s resident standards expert, Tom Richardson, and CEO, Lauren Stewart, have a forward-looking conversation about what’s next.
Link to recording, transcript, and accompanying resource: https://bnctechforum.ca/sessions/standardsgoals-for-2025-standards-certification-roundup/
Presented by BookNet Canada on May 6, 2025 with support from the Department of Canadian Heritage.
Mastering Advance Window Functions in SQL.pdfSpiral Mantra
How well do you really know SQL?📊
.
.
If PARTITION BY and ROW_NUMBER() sound familiar but still confuse you, it’s time to upgrade your knowledge
And you can schedule a 1:1 call with our industry experts: https://spiralmantra.com/contact-us/ or drop us a mail at [email protected]
Book industry standards are evolving rapidly. In the first part of this session, we’ll share an overview of key developments from 2024 and the early months of 2025. Then, BookNet’s resident standards expert, Tom Richardson, and CEO, Lauren Stewart, have a forward-looking conversation about what’s next.
Link to recording, presentation slides, and accompanying resource: https://bnctechforum.ca/sessions/standardsgoals-for-2025-standards-certification-roundup/
Presented by BookNet Canada on May 6, 2025 with support from the Department of Canadian Heritage.
Generative Artificial Intelligence (GenAI) in BusinessDr. Tathagat Varma
My talk for the Indian School of Business (ISB) Emerging Leaders Program Cohort 9. In this talk, I discussed key issues around adoption of GenAI in business - benefits, opportunities and limitations. I also discussed how my research on Theory of Cognitive Chasms helps address some of these issues
Technology Trends in 2025: AI and Big Data AnalyticsInData Labs
At InData Labs, we have been keeping an ear to the ground, looking out for AI-enabled digital transformation trends coming our way in 2025. Our report will provide a look into the technology landscape of the future, including:
-Artificial Intelligence Market Overview
-Strategies for AI Adoption in 2025
-Anticipated drivers of AI adoption and transformative technologies
-Benefits of AI and Big data for your business
-Tips on how to prepare your business for innovation
-AI and data privacy: Strategies for securing data privacy in AI models, etc.
Download your free copy nowand implement the key findings to improve your business.
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdfAbi john
Analyze the growth of meme coins from mere online jokes to potential assets in the digital economy. Explore the community, culture, and utility as they elevate themselves to a new era in cryptocurrency.
Web & Graphics Designing Training at Erginous Technologies in Rajpura offers practical, hands-on learning for students, graduates, and professionals aiming for a creative career. The 6-week and 6-month industrial training programs blend creativity with technical skills to prepare you for real-world opportunities in design.
The course covers Graphic Designing tools like Photoshop, Illustrator, and CorelDRAW, along with logo, banner, and branding design. In Web Designing, you’ll learn HTML5, CSS3, JavaScript basics, responsive design, Bootstrap, Figma, and Adobe XD.
Erginous emphasizes 100% practical training, live projects, portfolio building, expert guidance, certification, and placement support. Graduates can explore roles like Web Designer, Graphic Designer, UI/UX Designer, or Freelancer.
For more info, visit erginous.co.in , message us on Instagram at erginoustechnologies, or call directly at +91-89684-38190 . Start your journey toward a creative and successful design career today!
1. Object Oriented Programming Using C++
Prepared by
Mr. Nagesh Pratap Singh
JMS GROUP OF INSTITUTIONS, HAPUR
DEPARTMENT OF COMPUTER APPLICATION
2. C++ FUNCTIONS
A function is a block of code which only runs when it is
called.
You can pass data, known as parameters, into a
function.
Functions are used to perform certain actions, and they
are important for reusing code: Define the code once, and
use it many times
3. • The main( )Functon ;
ANSI does not specify any return type for the main ( ) function which is the starting point for the execution of a
program .
The definition of main( ) is :-
main()
{
//main program statements
}
This is property valid because the main () in ANSI C does not return any value. In C++, the main () returns a
value of type int to the operating system.
The functions that have a return value should use the return statement for terminating.
The main () function in C++ is therefore defined as follows.
int main( )
{
--------------
--------------
return(0)
}
Since the return type of functions is int by default, the key word int in the main( ) header is optional
4. INLINE FUNCTION:
• To eliminate the cost of calls to small functions C++ proposes a new feature called inline function. An inline
function is a function that is expanded inline when it is invoked .That is the compiler replaces the function
call with the corresponding function code.
The inline functions are defined as follows:-
inline function-header
{
function body;
}
Example:
inline double cube (double a)
{
return(a*a*a);
}
The above inline function can be invoked by statements like c=cube(3.0);
d=cube(2.5+1.5);
remember that the inline keyword merely sends a request, not a command to the compliler. The compiler may
ignore this request if the function definition is too long or too complicated and compile the function as a normal
function
5. SOME OF THE SITUATIONS WHERE
INLINE EXPANSION MAY NOT WORK ARE:
1. For functions returning values if a loop, a switch or a go
to exists.
2. for function s not returning values, if a return statement
exists.
3. if functions contain static variables.
4. if inline functions are recursive,.
7. DEFAULT ARGUMENTS IN C++
• A default argument is a value provided in a function
declaration that is automatically assigned by the compiler
if the calling function doesn’t provide a value for the
argument. In case any value is passed, the default value
is overridden.
• 1) The following is a simple C++ example to demonstrate
the use of default arguments. Here, we don’t have to write
3 sum functions; only one function works by using the
default values for 3rd and 4th arguments.
9. CPP PROGRAM TO DEMONSTRATE DEFAULT
ARGUMENTS
#include <iostream>
using namespace std;
int sum(int x, int y, int z = 0, int w = 0)
{
return (x + y + z + w);
}
int main()
{
cout << sum(10, 15) << endl;
cout << sum(10, 15, 25) << endl;
cout << sum(10, 15, 25, 30) << endl;
return 0;
}
10. FUNCTION OVERLOADING:
Function overloading is a feature of object-oriented
programming where two or more functions can have the
same name but different parameters. When a function
name is overloaded with different jobs it is called Function
Overloading. In Function Overloading “Function” name
should be the same and the arguments should be
different. Function overloading can be considered as an
example of a polymorphism feature in C++.
If multiple functions having same name but parameters of
the functions should be different is known as Function
Overloading.
12. #include <iostream>
using namespace std;
void add(int a, int b)
{
cout << "sum = " << (a + b);
}
void add(double a, double b)
{
cout << endl << "sum = " << (a + b);
}
int main()
{
add(10, 2);
add(5.3, 6.2);
return 0;
}
14. WHY USE CLASSES INSTEAD OF STRUCTURES
• Classes and structures are somewhat the same but
still, they have some differences. For example, we
cannot hide data in structures which means that
everything is public and can be accessed easily which
is a major drawback of the structure because structures
cannot be used where data security is a major concern.
Another drawback of structures is that we cannot add
functions in it.
15. CLASSES IN C++
• Class is a group of objects that share common
properties and relationships .In C++, a class is a
new data type that contains member variables
and member functions that operates on the
variables. A class is defined with the keyword
class. It allows the data to be hidden, if
necessary from external use.
16. CLASS DECLARATION
Generally a class specification has two parts:-
a) Class declaration
b) Class function definition
Syntax:-
class class-name
{
private:
variable declarations;
function declaration ;
public:
variable declarations;
function declaration;
};
17. CREATING OBJECTS:
Once a class has been declared we can create variables of that type by using the
class name.
Example: item x;
creates a variables x of type item.
In C++, the class variables are known as objects. Therefore x is called an object of
type item.
item x, y ,z also possible.
class item
{ -----------
-----------
----------- }x ,y ,z;
would create the objects x ,y ,z of type item.
18. ACCESSING CLASS MEMBER:
The private data of a class can be accessed only through the member functions of that class.
The main() cannot contains statements that the access number and cost directly.
Syntax: object name.function-name(actual arguments);
Example:- x. getdata(100,75.5);
It assigns value 100 to number, and 75.5 to cost of the object x by implementing the getdata() function .
Example: class xyz {
Int x;
Int y;
public:
int z;
};
xyz p;
p. x =0; error , x is private
p. z=10; ok ,z is public
19. DEFINING MEMBER FUNCTION:
Member can be defined in two places
• Outside the class definition
• Inside the class function
OUTSIDE THE CLASS DEFlNATION :-
Member function that are declared inside
a class have to be defined separately
outside the class.Their definition are very
much like the normal functions.
Syntax:
return type class-name::function-name(argument
declaration )
{
function-body
}
Example:
void item :: getdata (int a , float b )
{
number=a; cost=b;
}
void item :: putdata ( void)
{
cout<<”number=:”<<number<<endl;
cout<<”cost=”<<cost<<endl;
}
20. INSIDE THE CLASS DEFINATION:
Another method of defining a member function is to replace the function declaration by the
actual function definition inside the class .
Example:
class item
{
public:
Intnumber; float cost;
void getdata (int a ,float b);
void putdata(void)
{
cout<<number<<endl; cout<<cost<<endl;
}
};
21. A C++ PROGRAM WITH CLASS:
# include< iostream. h>
class item
{
public:
int number; float cost;
void getdata ( int a , float b);
void putdala ( void)
{
cout<<“number:”<<number<<endl;
cout<<”cost :”<<cost<<endl;
}
};
void item :: getdata (int a , float b)
{
number=a; cost=b;
}
main ( )
{
item x;
cout<<”nobjectx”<<endl;
x. getdata(100,299.95);
x .putdata();
item y;
cout<<”n object y”<<endl;
y. getdata(200,175.5);
y. putdata();
}