We are here with you hands in hands to facilitate your learning & don't appreciate the idea of copying or replicating solutions. Read More>>

Looking For Something at vustudents.ning.com? Click Here to Search

www.bit.ly/vucodes

+ Link For Assignments, GDBs & Online Quizzes Solution

www.bit.ly/papersvu

+ Link For Past Papers, Solved MCQs, Short Notes & More


Dear Students! Share your Assignments / GDBs / Quizzes files as you receive in your LMS, So it can be discussed/solved timely. Add Discussion

How to Add New Discussion in Study Group ? Step By Step Guide Click Here.

Correct and Final Solution of Assignment No.4 Of CS201 Due Date 28 Jan 2013 Solved by Waqas Rasheed

+ How to Follow the New Added Discussions at Your Mail Address?

+ How to Join Subject Study Groups & Get Helping Material?

+ How to become Top Reputation, Angels, Intellectual, Featured Members & Moderators?

+ VU Students Reserves The Right to Delete Your Profile, If?


See Your Saved Posts Timeline

Views: 2658

.

+ http://bit.ly/vucodes (Link for Assignments, GDBs & Online Quizzes Solution)

+ http://bit.ly/papersvu (Link for Past Papers, Solved MCQs, Short Notes & More)

+ Click Here to Search (Looking For something at vustudents.ning.com?)

+ Click Here To Join (Our facebook study Group)

Replies to This Discussion

CCP ki file mein upload kr 2 koi q itni mehnat krwani hai dosto ...!!!

waqas bhai yahan kia likha hai

agar ap ko ye statment nahi smaaj arahi to ap meri output window mai se dekh lo k kya likha hoa..

Circle ma yah likha hai (After adding both objects by using overloaded + operator) 

#include <iostream.h>
class Location
{
int l1;
int l2;
public:
Location();
Location(int lon,int lat);
void view();
Location operator ++();
Location operator --();
void* operator new (size_t size);
void operator delete( void * ptr );
};

Location::Location()
{
l1=0;
l2=0;
}

Location::Location(int lon,int lat)
{
l1=lon;
l2=lat;
}
void Location::view()
{
coutendl"Longitude : "l1endl;
cout"Latitude : "l2endlendl;
}
void* Location::operator new(size_t size)
{
cout"Overloaded new operator called....." endl;
void * rtn = malloc (size ) ;
return rtn;
}
Location Location::operator ++()
{
++l1;
++l2;
}
Location Location::operator --()
{
--l1;
--l2;
}
void Location :: operator delete( void *memory )
{
cout"Overload delete operator called....."endlendl;
free( memory );
}

main()
{

system("cls");
Location l1(10,20), *l2= new Location(30,40);

coutendl"Coordinates for Location 1:";
l1.view();
++l1;
cout"After applying overloaded ++ operator on Location 1 : ";
l1.view();
cout"Coordinates for Location 2:";
l2[0].view();
--l2[0];
cout"After applying overloaded -- operator on Location 2 : ";
l2[0].view();
delete l2;
system("pause");
}

#include <iostream.h>
class Location
{
int l1;
int l2;
public: 
Location(); 
Location(int lon,int lat); 
void view(); 
Location operator ++(); 
Location operator --(); 
void* operator new (size_t size); 
void operator delete( void * ptr ); 
};

Location::Location()
{
l1=0;
l2=0;
}

Location::Location(int lon,int lat) 
{
l1=lon;
l2=lat;
}
void Location::view() 
{
coutendl"Longitude : "l1endl;
cout"Latitude : "l2endlendl;
}
void* Location::operator new(size_t size) 
{
cout"Overloaded new operator called....." endl; 
void * rtn = malloc (size ) ; 
return rtn; 
}
Location Location::operator ++()
{
++l1;
++l2;
}
Location Location::operator --()
{
--l1;
--l2;
}
void Location :: operator delete( void *memory )

cout"Overload delete operator called....."endlendl;
free( memory );
}

main()
{

system("cls");
Location l1(10,20), *l2= new Location(30,40);

coutendl"Coordinates for Location 1:"; 
l1.view();
++l1; 
cout"After applying overloaded ++ operator on Location 1 : ";
l1.view(); 
cout"Coordinates for Location 2:"; 
l2[0].view(); 
--l2[0]; 
cout"After applying overloaded -- operator on Location 2 : ";
l2[0].view(); 
delete l2;
system("pause");
}

C++ Class Definitions:
When you define a class, you define a blueprint for a data type. This doesn't actually define any data, but it does define what the class name means, that is, what an object of the class will consist of and what operations can be performed on such an object.

A class definition starts with the keyword class followed by the class name; and the class body, enclosed by a pair of curly braces. A class definition must be followed either by a semicolon or a list of declarations. For example we defined the Box data type using the keyword class as follows:

class Box
{
public:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};
The keyword public determines the access attributes of the members of the class that follow it. A public member can be accessed from outside the class anywhere within the scope of the class object. You can also specify the members of a class as private or protected which we will discuss in a sub-section.

Define C++ Objects:
A class provides the blueprints for objects, so basically an object is created from a class. We declare objects of a class with exactly the same sort of declaration that we declare variables of basic types. Following statements declare two objects of class Box:

Box Box1; // Declare Box1 of type Box
Box Box2; // Declare Box2 of type Box

The Class Constructor:
A class constructor is a special member function of a class that is executed whenever we create new objects of that class.

A constructor will have exact same name as the class and it does not have any return type at all, not even void. Constructors can be very useful for setting initial values for certain member variables.

Following example explain the concept of constructor:

#include 

using namespace std;

class Line
{
public:
void setLength( double len );
double getLength( void );
Line(); // This is the constructor

private:
double length;
};

// Member functions definitions including constructor
Line::Line(void)
{
cout "Object is being created" endl;
}

void Line::setLength( double len )
{
length = len;
}

double Line::getLength( void )
{
return length;
}
// Main function for the program
int main( )
{
Line line;

// set line length
line.setLength(6.0); 
cout "Length of line : " line.getLength() br/>When the above code is compiled and executed, it produces following result:

Object is being created
Length of line : 6
Parameterized Constructor:
A default constructor does not have any paramter but if you need a constructor can have parameters. This helps you to assign initial value to an object at the time of its creation as shown in the following example:

#include 

using namespace std;

class Line
{
public:
void setLength( double len );
double getLength( void );
Line(double len); // This is the constructor

private:
double length;
};

// Member functions definitions including constructor
Line::Line( double len)
{
cout "Object is being created, length = " len endl;
length = len;
}

void Line::setLength( double len )
{
length = len;
}

double Line::getLength( void )
{
return length;
}
// Main function for the program
int main( )
{
Line line(10.0);

// get initially set length.
cout "Length of line : " line.getLength() br/>When the above code is compiled and executed, it produces following result:

Object is being created, length = 10
Length of line : 10
Length of line : 6
Using Initialization Lists to Initialize Fields:
In case of paramterized constructor, you can use following syntax to initialize the fields:

Line::Line( double len): length(len)
{
cout "Object is being created, length = " len endl;
}
Above syntax is equal to the following syntax:

Line::Line( double len)
{
cout "Object is being created, length = " len endl;
length = len;
}
If for a class C, you have multiple fields X, Y, Z etc to be initialize then use can use same syntax and separate the fields by comma as follows:

C::C( double a, double b, double c): X(a), Y(b), Z(c)
{
....
}
The Class Destructor:
A destructor is a special member function of a class that is executed whenever an object of it's class goes out of scope or whenever the delete expression is applied to a pointer to the object of that class.

A destructor will have exact same name as the class prefixed with a tilde (~) and it can neither return a value nor can it take any parameters. Destructor can be very useful for releasing resources before coming out of the program like closing files, releasing memories etc.

Following example explain the concept of destructor:

#include 

using namespace std;

class Line
{
public:
void setLength( double len );
double getLength( void );
Line(); // This is the constructor declaration
~Line(); // This is the destructor: declaration

private:
double length;
};

// Member functions definitions including constructor
Line::Line(void)
{
cout "Object is being created" endl;
}
Line::~Line(void)
{
cout "Object is being deleted" endl;
}

void Line::setLength( double len )
{
length = len;
}

double Line::getLength( void )
{
return length;
}
// Main function for the program
int main( )
{
Line line;

// set line length
line.setLength(6.0); 
cout "Length of line : " line.getLength() br/>When the above code is compiled and executed, it produces following result:

Object is being created
Length of line : 6
Object is being deleted

Getters and Setters


Getters and Setters allow you to effectively protect your data. This is a technique used greatly when creating classes. For each variable, a get method will return its value and a set method will set the value.

You may ask why you shouldn't just edit the value directly. The reason for this is twofold. Firstly, if you decide that some action should be taken every time you change the value of a particular variable, you only need to edit the set method instead of looking for every place you change the variable. Another reason is that a person using your code can look for all methods starting with get or set instead of looking for specific variables that they need to remember.

Contents of main.cpp :

#include <iostream>
#include <stdlib.h>

using namespace std;

class Player
{
The getters and setters are usually public and the variables are made private.

public :

void attack()
{ cout "Player Attacks" endl; }
Getters usually start off with the letters "get". You may have noticed the const keyword after the method below. This states that the method will not change any value within the class. This is useful as it forces you not to modify any value of the class. Getters usually never modify values of the class and so you can therefore place a const after the method signature.

// Getters
int getHealth() const { return health; }
int getMana() const { return mana; }
Setters are similar and they start with the "set" characters. The functions take a parameter which specifies the new value. Notice that the const keyword isn't used as you are changing a value within the class.

// Setters
void setHealth(int h) { health = h; }
void setMana(int m) { mana = m; }
The private data variables are given below.

private :

int health;
int mana;
};
An example of using a setter and a getter is given below.

int main()
{
Player grant;

grant.setHealth(30);

cout grant.getHealth() endl;

system("pause");

return 0;
}
Getters and setters are a common technique used in object orientated programming. It is good practice to use these getters and setters instead of changing the variables directly.

C++ Friend Functions
A friend function of a class is defined outside that class's scope but it has the right to access all private and protected members of the class. Even though the prototypes for friend functions appear in the class definition, friends are not member functions.

A friend can be a function, function template, or member function, or a class or class template, in which case the entire class and all of its members are friends.

To declare a function as a friend of a class, precede the function prototype in the class definition with keyword friend as follows:

class Box
{
double width;
public:
double length;
friend void printWidth( Box box );
void setWidth( double wid );
};
To declare all member functions of class ClassTwo as friends of class ClassOne, place a following declarationin the definition of class ClassOne:

friend class ClassTwo;
Consider the following program:

#include 

using namespace std;

class Box
{
double width;
public:
friend void printWidth( Box box );
void setWidth( double wid );
};

// Member function definition
void Box::setWidth( double wid )
{
width = wid;
}

// Note: printWidth() is not a member function of any class.
void printWidth( Box box )
{
/* Because setWidth() is a friend of Box, it can
directly access any member of this class */
cout "Width of box : " box.width br/>When the above code is compiled and executed, it produces following result:

Width of box : 10

Relational operators overloading in C++

There are various relational operators supported by C++ language like (<, >, <=, >=, ==, etc.) which can be used to compare C++ built-in data types.

You can overload any of these operators, which can be used to compare the objects of a class.

Following example explain how a < operator can be overloaded and similar way you can overload other relational operators.

#include 
using namespace std;

class Distance
{
private:
int feet; // 0 to infinite
int inches; // 0 to 12
public:
// required constructors
Distance(){
feet = 0;
inches = 0;
}
Distance(int f, int i){
feet = f;
inches = i;
}
// method to display distance
void displayDistance()
{
cout "F: " feet " I:" inches br/>When the above code is compiled and executed, it produces following result:

D2 is less than D1

Formula of area of trapezium:

Area = (1/2) * (a + b) * h

C code:

#include <stdio.h>

int main(){

float b1,b2,h;
float area;

printf("Enter the size of two bases and height of the trapezium : ");
scanf("%f%f%f",&b1,&b2,&h);

area = 0.5 * ( b1 + b2 ) * h ;

printf("Area of trapezium is: %.3f",area);

return 0;
}

Sample output:

Enter the size of two bases and height of the trapezium: 5 8 3
Area of trapezium is: 19.500

RSS

Today Top Members 

© 2020   Created by + M.Tariq Malik.   Powered by

Promote Us  |  Report an Issue  |  Privacy Policy  |  Terms of Service

.