[go: up one dir, main page]

0% found this document useful (0 votes)
176 views9 pages

C++ Nelson Notes Unit 3

Class is a user-defined data type that binds data and member functions together. A class declaration specifies data members and member functions, but does not allocate memory for objects. Memory is allocated when objects are instantiated from the class. There are two ways to define member functions: inside the class or outside using a scope resolution operator. Constructors initialize objects and can be default, parameterized, or copy constructors. Destructors destroy objects and are called automatically when objects go out of scope. Operator overloading allows operators to work with user-defined types, while function overloading defines multiple functions with the same name but different parameters. Type conversion changes data from one type to another, which can be implicit via compiler or explicit through user-

Uploaded by

Nelson Raja
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
176 views9 pages

C++ Nelson Notes Unit 3

Class is a user-defined data type that binds data and member functions together. A class declaration specifies data members and member functions, but does not allocate memory for objects. Memory is allocated when objects are instantiated from the class. There are two ways to define member functions: inside the class or outside using a scope resolution operator. Constructors initialize objects and can be default, parameterized, or copy constructors. Destructors destroy objects and are called automatically when objects go out of scope. Operator overloading allows operators to work with user-defined types, while function overloading defines multiple functions with the same name but different parameters. Type conversion changes data from one type to another, which can be implicit via compiler or explicit through user-

Uploaded by

Nelson Raja
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Write about the Class and Procedure to Define a Class in C++?

Define Class:
 Class is collection of similar types of objects. Also, Class is a user defined data type.
Class is used to binds data and member functions.
 When a class is defined, no memory is allocated but when it is instantiated (i.e. an object
is created) memory is allocated.
 The general syntax or general form of the class declaration is as follows:

Defining Class
The class specification can be done in two part:
(i) Class definition. It describes both data members and member functions.
(ii) Class method definitions. It describes how certain class member functions are coded.
In C++, the member functions can be coded in two ways :
(a) Inside class definition
(b) Outside class definition using scope resolution operator (::)
Inside Class Definition:
When a member function is defined inside a class, we do not require placing a membership label
along with the function name. We use only small functions inside the class definition and such
functions are known as inline functions.
Outside Class Definition Using Scope Resolution Operator (::)
In this case the function’s full name (qualified_name) is written as shown:
Name_of_the_class :: function_name
The syntax for a member function definition outside the class definition is :
return_type name_of_the_class::function_name (argument list)
{
body of function
}
Here the operator::known as scope resolution operator helps in defining the member function
outside the class.

DECLARATION OF OBJECTS:
The objects of a class are declared after the class definition.
Syntax
Class_name object;
Example
test obj1;

Example
class test
{
public:
string geekname;
int id;
// printname is not defined inside class defination
void printname();
// printid is defined inside class defination
void printid()
{
cout << "Geek id is: " << id;
}
};
// Definition of printname using scope resolution operator::
void test::printname()
{
cout << "Geekname is: " << geekname;
}
int main() {
test obj1;
obj1.geekname = "xyz";
obj1.id=15;
obj1.printname();
cout << endl;
obj1.printid();
return 0;
}

Explain about the Constructors and its types with example?


 A constructor is a special member function and used to initialize the objects of its class
 Also, Constructor name is same as the Class Name.
The constructor functions have some special characteristics.
 Constructor should be declared in the public section.
 Constructor are invoked automatically when the objects are created.
 Constructor do not have return types, not even void and therefore, they cannot return
values.
 Constructor cannot be inherited, though a derived class can call the base class
constructor.
Type Of Constructor
1. Default constructor
2. Parameterized Constructor
3. Copy Constructors

1) Default Constructor
A default constructor doesn’t have any parameters (or arguments )
Example Program for Default Constructor
#include <iostream.h>
#include <conio.h>
class student_info
{

public:
//Default constructor

student_info()
{
cout<<" Welcome to Student Info Constructor Program "<<endl;
}
};
void main()
{
student_info obj1;
}

Output: Welcome to Student Info Constructor Program


2) Parameterized Constructor
Constructors with parameters are known as Parameterized constructors. These type of
constructor allows us to pass arguments while object creation.

Example Program for Parameterized Constructor


#include <iostream.h>
#include <conio.h>

class Add
{
public:
//Parameterized constructor
Add(int num1, int num2)
{
cout<<(num1+num2)<<endl;
}

};

void main()
{
Add obj(10, 20);
}

Output is : 30

Copy constructor
Copy constructor is used to declare and initialize an object from another object.

class A
{
public:
int x;
A(int a) // parameterized constructor.
{
x=a;
}
A(A &i) // copy constructor
{
x = i.x;
}
};
int main()
{
A a1(20); // Calling the parameterized constructor.
A a2(a1); // Calling the copy constructor.
cout<<a2.x;
return 0;
}

Explain about Destructors with example program?


A destructor works just opposite to constructor.
Destructor is used to destroy (or delete) the object.

~class_name()
{
//Some code
}
Similar to constructor, the destructor name should exactly match with the class name. A
destructor declaration should always begin with the tilde(~) symbol as shown in the syntax
above.

When does the destructor get called?


A destructor is automatically called when:
1) The program finished execution.
2) When a scope (the { } parenthesis) containing local variable ends.
3) When you call the delete operator.

class Employee
{
public:
Employee()
{
cout<<"Constructor Invoked"<<endl;
}
~Employee()
{
cout<<"Destructor Invoked"<<endl;
}
};

void main()
{
Employee e1;

Define Overloading?
C++ allows you to specify more than one definition for a function name or an operator in the
same scope, which is called function overloading and operator overloading respectively.

Explain about the Function Overloading?


Function overloading is a C++ programming feature.
Another name of Function overloading is, compile-time polymorphism.

C++ allow us to create two or more functions with the same name and different parameters.

In this below program, function name sum is defined in two places with different parameters.

Example Program for Function Overloading:

class Addition
{
public:
int sum(int num1,int num2) {
return num1+num2;
}
int sum(int num1,int num2, int num3) {
return num1+num2+num3;
}
};
void main()
{
Addition obj;
cout<<obj.sum(20, 15)<<endl;
cout<<obj.sum(81, 100, 10);
}
Explain about Operator Overloading in C++?
 In C++, we can make operators to work for user defined classes.
 Overloaded operator is used to perform operation on user-defined data type.
 For example, we can overload an operator ‘+’ to concatenate two strings.

Operator that are not overloaded are follows

Example Program for Operator Overloading with Unary Operator

#include <iostream>
#include <conio.h>
using namespace std;

class example
{
int a,;
public:
void input()
{
cout<<"Enter the number - ";
cin>>a;
cout<<endl;

}
void operator -() //operator function as a member function
{
a=-a;
}
void display()
{
cout<<"a="<<a<<endl;
}
};
int main()
{
example e;
e.input();
cout<<"Before overloading unary minus operator"<<endl;
e.display();
-e;
cout<<"After overloading unary minus operator"<<endl;
e.display();
getch();
return 0;
}

Explain about Type Conversion in C++?


The process of converting data from one data type into another data types is called type
conversion.

Example:
float a=3.456
int j=a; (Here Variable a is in Float data type and Variable J is Integer data type.
So, Automatic conversion process convert the float into integer data type)

There are Two types in Type Conversion

1. Implicit Type Conversion


 Done by the compiler on its own, without any external trigger from the user.
 Generally takes place when in an expression more than one data type is present.

2. Explicit Type Conversion


it is user-defined one. There are three types in Explicit conversion.
I. Conversion from basic data type to user-defined data type (class type)
II. Conversion from class type to basic data type
III. Conversion from one class type to another class type

i. Conversion from basic data type to user-defined data type


The conversion from basic to class type is easily carried out.
It is automatically done by the compiler with the help of in-built routines or by applying type
casting.
ii. Conversion from Class Type to Basic Data
In this type, The compiler does not have any knowledge about the user-defined data type built
using classes. In this type of conversion, the programmer needs to explicitly tell the compiler
how to perform conversion from class to basic type. These instructions are written in a
member function.

iii. One Class Type to Another Class Type


There are two ways to convert object data type from one class to another. One is to define a
conversion operator function in source class or a one-argument constructor in a destination class

You might also like