- Get link
- X
- Other Apps
Classes in C language -
It is a user defined data type data type which hold its own data members and member function which can be accessed and user by creating an instance of that class and we say that class is building block that leads to Object Oriented programming
Data member - It is the data variable of the function
Member function - It are the function use to manipulate these function variable
For Example -
A car is an object the car has attribute such as weight , color and methods such as drive and brake
Attribute and method are basically variable and function that belong to class
syntax -
class class{ // The class
public: // Access specifier
int Num; // Attribute (int variable)
string word; // Attribute (string variable)
};
public: // Access specifier
int Num; // Attribute (int variable)
string word; // Attribute (string variable)
};
Program -
// class explaination
#include<iostream>
using namespace std;
class classname
{
public:
string mystring;
void printfun()
{
cout << "Geekname is : " << mystring;
}
};
int main() {
classname obj1;
// accessing datamember
obj1.mystring = "Hello world";
// accessing member funcation
obj1.printfun();
return 0;
}
Object in C language -
C++ is an object-oriented programming language. Everything in C++ is associated with classes and objects, along with its attributes and methods. For example: in real life, a car is an object. The car has attributes, such as weight and color, and methods, such as drive and brake
Program -
// Object in c++
#include<iostream>
using namespace std;
class Classname{ // class
public: //Access specifier
int num; //Attrbute (int variable)
string mystring; //Attribute(String variable)
};
int main()
{
Classname obj; // create an object of myclass
//Access attribute and set value
obj.num = 12;
obj.mystring = "hello string";
// print attribute values
cout<< "Number is " <<obj.num << endl;
cout<< "String is " <<obj.mystring << endl;
return 0;
}
/* Output -
Number is 12
String is hello string */
There are two for defining class -
1. Inside class definition
2. Outside class definition
Program -
// Inside or outside class defination
#include<iostream>
using namespace std;
class classname{
public:
string name;
int id;
// printname is not defined inside class defination
// Outside class definition
void printname();
// printid is defined inside class defination
void printid(){
cout<<"Class id is = "<< id ;
}
};
// Defination of printname using Scope resolution operator (::)
void classname::printname()
{
cout << "Class name is : "<< name;
}
int main(){
classname obj1;
cout<<"Enter the name : ";
cin>>obj1.name;
obj1.id=15;
obj1.printname();
cout<< endl;
obj1.printid();
return 0;
}
/* Output -
Enter the name : name
Class name is : name
Class id is = 15
*/
How Function overloading work -
Function Overloading :-
Using of funcation in different place in program with same name or the process of have two or more function with the same name but parameter are different called function overloading.
Simple program of function overloading -
#include<iostream>
using namespace std;
void integer(int a){
cout<<"Here is int" << a <<endl;
}
void decimal(float b){
cout<<"here is float"<< b <<endl;
}
int main(){
integer(10);
decimal(10.10);
return 0;
}
// output -
// Here is int 10
// here is float 10.1
Function overloading with return type -
#include<iostream>
using namespace std;
class addition{
public:
int sum(int n1, int n2) {
return n1+n2;
}
int sum(int n1, int n2, int n3) {
return n1+n2+n3;
}
};
int main(){
addition add;
cout<<add.sum(2,4)<<endl;
cout<<add.sum(2,4,5);
return 0;
}
// Output -
// 6
// 11
Function Overriding program -
// Function Overriding
#include<iostream>
using namespace std;
class BaseClass{
public:
virtual void Display()
{
cout<< "\n This is Display() method"
"of BaseClass";
}
void show()
{
cout<<"\nThis is show() method"
"of BaseClass";
}
};
class DerivedClass : public BaseClass
{
public:
void Display()
{
cout<<"\nThis is Display() method"
"of DerivedClass";
}
};
// Driver code
int main()
{
DerivedClass dr;
BaseClass &bs = dr;
bs.Display();
dr.show();
}
//Output-
// This is Display() method of Derived Class
//This is show() method of Base Class
Accessing data member -
Public data member are also accessed in the same way given however private data member are not allowed to accessed directly by object. accessing data member depends solely on access control of that data member
there are three type of ADM -
1. Public
2. Private
3. Protected
Comments
Post a Comment