Module 1 C++editted
Module 1 C++editted
Constructor in C++
Special method which invoke automatically during object creation
Has same name as class name
Syntax:
class_name(parameters list){
// constructor defintion
}
Has no return type
Types: 1.Default
2. parameterized
Example for default constructor
#include<iostream.h>
class myclass{ //class declaration
public:
myclass() //default constructor
{
cout<<“\n default constructor”;
}
};
int main(){
myclass obj1; //creating object 1;
myclass obj2; //creating object 2;
return 0;
}
OUTPUT:
default constructor //it is displayed when object1 is created
default constructor //it is displayed when object2 is created
Parameterized Constructors
pass arguments to constructors.
initialize an object when it is created.
add parameters to it the way you would to any other
function.
define the constructor's body use the parameters
to initialize the object
Syntax: 1. Constructor_name obj_name(arguments);
myclass::myclass(int id) {
cout << "Initializing " << id << "\n"; who = id;
}
myclass::~myclass() {
cout << "Destructing " << who << "\n";
}
int main() {
myclass local_ob1(3); //local objects
cout << "This will not be first line displayed.\n";
myclass local_ob2(4);
return 0;
}
OUTPUT:
Initializing 1
Initializing 2
Initializing 3
Initializing 4
Destructing 4
Destructing 3
Destructing 2
Destructing 1
Explanation:
1.After class creation two global objects, glob_ob1 and glob_ob2, are created outside any function. They are
instances of myclass with initial values 1 and 2.When these objects are created, the constructor is called for
each, and “Initializing 1” and “Initializing 2” will be printed.
2.The constructor initializes the who member with the provided id and prints a message indicating the
initialization.
3. The destructor prints a message indicating the destruction of the object and the value of who.
4. Inside the main function, two local objects, local_ob1 and local_ob2, are created with initial values 3 and
4.
5. When these local objects go out of scope (at the end of the main function), their destructors are called,
class.
to override function in the Inheritance.
#include <iostream.h>
int main ()
{
// declare local variable
int num = 100;
class Student {
private: int rollNo;
char name[10];
int marks;
public:
static int objectCount;
Student() {
objectCount++; }
void getdata() {
cout << "Enter roll number: "<<endl;
cin >> rollNo;
cout << "Enter name: "<<endl;
cin >> name;
cout << "Enter marks: "<<endl;
cin >> marks;
}
void putdata() {
cout<<"Roll Number = "<< rollNo <<endl;
cout<<"Name = "<< name <<endl;
cout<<"Marks = "<< marks <<endl;
cout<<endl;
} };
int Student::objectCount = 0;
int main(void) {
Student s1;
s1.getdata();
s1.putdata();
Student s2;
s2.getdata();
s2.putdata();
int main ()
{
cout << " The value of the num is: " << Note:: func () << endl;
return 0;
}
Inline function
to reduce the function call overhead.
increase efficiency if it is small
any changes made to an inline function will
Syntax:
inline return_type function_name(parameters)
{
// function code }
Not used in
loop. (for, while, do-while)
if a function has static variables.
function recurses.
If the return statement is absent from the
statement.
#include <iostream>
class Student {
public: double marks;
Student(double m) {
marks = m;
}
};
calculateAverage(student1, student2);
return 0;
}
#include <iostream.h>
class Student {
public: double
marks1, marks2;
};
Student createStudent() {
Student student;
student.marks1 = 96.5;
student.marks2 = 75.0;
cout << "Marks 1 = " << student.marks1 << endl;
cout << "Marks 2 = " << student.marks2 << endl;
return student;
}
int main() {
Student student1;
student1 = createStudent();
return 0;
}