You are on page 1of 3

CLASS:

NAME:
ATTRIBUTES:
FUNCTIONS:

SYNTAX:
name :Class Student
attributes {Id, Name}
methods: constructors, print function etc.

Student

Constructors: Allocates memories to attributes.


Defaults constructor (where there is no parameter) memory allocation
Parameterised Constructor () memory allocation plus initialise the memory location
- Constructor is a Special type of Function (Name is class name)
- No return Type (normal functions do have return type)

int sum (int x, int y);


{
int z = x+y;
return z;
}

class Student {
public :
Student () {

Cout<<"Student Created\n";
}

};
void main () {
Student st;
}

- Implicit Call (as you make object, it is implicitly called) person p; student st;

- Same name as class (exactly same name)


- Normally used for initialization purpose
- Can be overloaded with parameters (function overloading, constructor overloading)
same name, same return type and different parameters
sum ()
Sum (a+b)
sum (a+b+c)

class Student {
public :
Student () {

// allocate memory, (1)garbage, (2) default 0, unknown, (3) what ever will be
needed at run time. out of first two only may exist.
if no constructor then default constructor is written by compiler.
Cout<<"Student Created\n";
}

};
void main () {
Student st;// function call in the main body of class

.........................................
Initialize Student Class Members Using Constructor

#include <iostream.h>
#include <string.h>
class Student {
private:
int id;
string name;
public :
Student (){
id=0;
name=“unknown";
}
void print() {
cout<<id<<endl;
cout<<name<<endl;
}

};
void main () {
Student st;
st.print();
}
...............................................
Constructor Overloading (input parameter)
#include <iostream.h>
#include <string.h>
class Student {
private:
int id;
string name;
public :
Student (){
id=0;
name=“unknown";
cout<<"Student Created\n";
}
Student(int d, string n){
id=d;
name=n;
cout<<"Student Created with Parameter\n";
}
Ivoid print() {
cout<<id<<endl;
cout<<name<<endl;
}

};
void main () {
Student st1;
st1.print();
Student st2(10,"Farooq");
st2.print();

}
...............................................

You might also like