What are Classes ?
Class is the basic concept of OOP's. Now here's a question,Â
What is OOP?
-> OOP stands for Object Oriented Programing.
-> Object Oriented Programming is about to creating objects that contains both data(variables) and functions.
-> In OOP Class and Objects are main aspects.
Advantages of OOP:-
1. It is faster and easier to execute programs with Classes and Objects.
2. OOP helps to keep the C++ code DRY "Don't Repeat Yourself", and makes the code easier to maintain, modify and debug.
3. OOP makes it possible to create full reusable applications with less code and shorter development time.
4. It provides a clear structure to the program.
Definition of Class:-
--> Class is a User-defined datatype . A class has some attributes and some functions. These are called as members of class. It works as a 'blueprint' for class objects.
-->Example:-
--> In Programming terms attributes are called as variables and functions are called as functions only.
-->In computer science, an object can be a variable, a data structure, a function, or a method, and as such, is a value in memory referenced by an identifier.
-->While declaring a class a "class" keyword is to be used and while declaring member functions and variables inside the class some access specifiers such as "public", "private", "protected" need to be specified.
-->By default the access specifier for all members is "public". Which means the members of class is available to whole scope of the program any member can access the members of any class.
--> Objects can contain data and code:Â
data in the form of fields (often known as attributes or properties or variables), and code, in the form of procedures (often known as methods or functions).
Syntax to Declare Class in C++ :-
# Declaring the class named as example
class example:
# code to be written
def fnc_name(self):
pass
# calling a class
example()
# if functions are used in a class,
# then we need to declare an object to the class to call it,
# object can be any variable.
obj = example()
# calling the function inside a class
example.fnc_name()
Syntax to create Objects of Class (calling class in main function) :-
Example :-
# Declaring class named as multiplication
class multiplication:
def multiply(self): # declaring a function
a = int(input("Enter First number\n"))
b = int(input("Enter Second number\n"))
c = a * b
print("The multiplication of both numbers is: ", c)
# specifying an object to class multi and calling it
obj = multiplication()
# calling the function inside the class
multiplication.multiply(obj)
Output
#ENJOY CODING
Post a Comment
FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP