0% found this document useful (0 votes)
11 views11 pages

Ass 2

The document contains code snippets demonstrating various C++ concepts: 1) Static data members using a static variable and static function. 2) Friend functions by defining a function as friend to access private members. 3) Constructors using default and parameterized constructors.

Uploaded by

Tejas Patil
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)
11 views11 pages

Ass 2

The document contains code snippets demonstrating various C++ concepts: 1) Static data members using a static variable and static function. 2) Friend functions by defining a function as friend to access private members. 3) Constructors using default and parameterized constructors.

Uploaded by

Tejas Patil
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/ 11

Roll No :7785

Assignment No :03
Assignment Name : A C++ program to demonstrate the static data members.
a)Static variable b)Static function

a)Static Variable
Program :
#include<iostream>

using namespace std;

class student

int Roll_No;

char Name[20];

static char Teacher_Name[20];

public:

void input()

cout<<"\n Enter Roll No:";

cin>>Roll_No;

cout<<"\n Enter Name:";

cin>>Name;

}
void output()

cout<<"\n Roll No is:"<<Roll_No;

cout<<"\n Name is:"<<Name;

};

char student::Teacher_Name[20]="ABC";

int main()

student s,s1;

s.input();

s1.input();

s.output();

s1.output();

return 0;

}
b)Static Function
Program :
#include<iostream>

using namespace std;

class student

int Roll_No;

char Name[20];

static char Teacher_Name[20];

public:

void accept()

cout<<"\n Enter Roll No:";

cin>>Roll_No;

cout<<"\n Enter Name:";

cin>>Name;

void display()

cout<<"\n Roll No is:"<<Roll_No;

cout<<"\n Name is:"<<Name;


}

static void display_Teacher()

cout<<"\n Teacher Name is:"<<Teacher_Name;

};

char student::Teacher_Name[20]="ABC";

int main()

student::display_Teacher();

student s,s1;

s.accept();

s1.accept();

s.display();

s1.display();

return 0;

}
Output:
Teacher Name is:ABC

Enter Roll No:03

Enter Name:Rajesh

Enter Roll No:20

Enter Name:Pratik

Roll No is:3

Name is:Rajesh

Roll No is:20

Name is:Pratik
Roll No :7785
Assignment No :04
Assignment Name :A C++ program to demonstrate the concept of friend
Function.

Program :
#include<iostream>

using namespace std;

class sample

public:

int a,b;

public:

void setvalue()

a=3;

b=20;

private:

friend float mean(sample s);

};

float mean(sample s)

{
return float(s.a+s.b)/2;

int main()

sample s1;

s1.setvalue();

cout<<"\n The mean value is:"<<mean(s1);

return 0;

Output :
The mean value is:11.5
Roll No :7785
Assignment No :05
Assignment Name :A C++ program to demonstrate the concept of constructor
a)Default constructor b)Parameterized constructor.

a)Default Constructor
Program :
#include<iostream>

using namespace std;

class student

public:

int Roll_No;

char Name[30];

public:

student()

cout<<"\n Enter Roll No:";

cin>>Roll_No;

cout<<"\n Enter Name:";

cin>>Name;

void display()
{

cout<<"\n Roll No is:"<<Roll_No;

cout<<"\n Name is:"<<Name;

};

int main()

student s;

s.display();

return 0;

Output :
Enter Roll No:3

Enter Name:Pratik

Roll No is:3

Name is:Pratik
a)Parameterized Constructor
Program :
#include<iostream>

using namespace std;

class student

public:

int Roll_No;

string Name;

public:

student(int r,string n)

Roll_No=r;

Name=n;

void display()

cout<<"\n Roll No is:"<<Roll_No;

cout<<"\n Name is:"<<Name;

};

int main()
{

student s(7809,"Pratik");

s.display();

return 0;

Output :
Roll No is:7809

Name is:Pratik

You might also like