C# :: How To Use Encapsulation / Polymorphism And Inheritance In WPF

Oct 20, 2014

I have been reading up about object oriented programming recently and have come across 'Encapsulation, Polymorphism and Inheritance' as i understand it so far all OOP programs should use these three concepts. So i started thinking how do i get these concepts into my program as i am using WPF C# and i could not really find much good info about how these concepts apply to WPF programs.

Or do these concepts just not work with WPF programs?

View 5 Replies


ADVERTISEMENT

C++ :: Object Inheritance And Encapsulation

Jan 17, 2014

I want to define a base class A which allows different type of logging (file stream, console, etc). In the meantime, I want a class B to to be able to call A's print function (i.e. derived class C's print function which defines the logging method). However, I don't want to make A's print function to be public because it will only be called by B.

class A {
protected:
virtual void print(void);
};

[Code] .....

View 1 Replies View Related

C# :: Inheritance Or Base Class / Polymorphism?

Sep 16, 2014

I have couple of objects which are using some amount of methods. Right now my application is not OOP and i am going to transfer it to OOP. So i would create each class for my each object. Almost all methods are in use for each object, the only one thing which is changing some of those objects passing not all parameters inside specific method. I can go two ways one is prepare interface for all methods i got and each of my classes could implement its own definition for it but other way almost all would implement exactly the same methods but with different parameters so maybe its better to create base class then for each object do inheritance from base class (polymorphism). inside base class i can prepare base methods and classes which will inherit from that class would override those methods for requirements they want.

So imagine: You got:

Memory
CPU
Latency

and all of them using mostly all of those same methods (only arguments for one of them could be use different way):

ExecuteQuery()
ExportToExcel()
PopulateDataTable()
PutValueToReport()

Now its better to do like this e.g:

Base class: Stuff
prop: name, id, date ...
methods to ovveride: ExecuteQuery(), ExportToExcel() ...

classes: CPU, Memory, Latency (inheriting from Stuff)
ovveride methods and align its definition for specific class use (remember mostly only passing args are used or not by specific class)

or go with inheritance

Interface SomeMethods
ExecuteQuery()
ExportToExcel()
PopulateDataTable()
PutValueToReport()

and each class would implemet its own definition.

The most important thing is that those every objects mostly using all of those methods and what is diffrence that one object can use all of available parameters inside this method and other one no. What i should do? Go with interface or inheritance and polymporfizm inside base class?

View 2 Replies View Related

C++ :: Virtual Breaks Encapsulation?

Jul 17, 2013

Here is the code,

Code:
class A {
public:
virtual void foo(){}
};
class B : public A {

[Code] ....

Obviously when you call pA->foo, it will call foo defined in B. But foo defined in B is private. It is not supposed to be called outside the class B. So it looks like virtual breaks encapsulation.

View 8 Replies View Related

C++ :: Difference Between Encapsulation And Data Abstraction?

Apr 19, 2013

what is main Difference between Encapsulation and Data Abstraction?

View 2 Replies View Related

C++ :: Why It Is Runtime Polymorphism

Apr 4, 2013

class Base
{
.....
.....
.....
virtual void display();

[code]....

in the above polymorphism why is it called runtime polymorphism when i can say seeing the code itself that display() function in derived gets executed with ptr->display(),so how does it become runtime polymorphism when i could get decide at compile itself ???

View 6 Replies View Related

C++ :: Calculate Tax Of Vehicle With Polymorphism?

Dec 29, 2012

How can we calculate tax of vehicle with polymorphism? There are 3 types of vehicles: long, commercial, and private vehicles' classes. There is a common class about all vehicles.

BASE class:
1.Brand and series name (such as Toyota Corrolla, Isuzu etc.)
2.Year of production (you can term it model)
3.Engine size in dm3
4.Owner’s name (including surname) and identity
5.Plate number.

COMMERCİAL :
1.Number of seats (apart from the driver)
2.Data to indicate whether the commercial vehicle is allowed to operate at night.

LONG :
1.The tonnage (max load in tons)
2.Data to indicate whether the long vehicle is allowed to carry goods internationally.

PRİVATE:
1.The class label which can be one of {A,B,C} .

View 1 Replies View Related

C++ :: Virtual Functions And Polymorphism

May 2, 2014

if we do a virtual functions(polymorphism) why we need re-declare the functions(when we create a new class derived from other)?

View 10 Replies View Related

C++ :: Does Polymorphism Affects Operators Overloading?

Nov 24, 2013

I must overload [] but at the same time I must use polymorphism. I wonder if using polymorphism affects operators overloading since when I modified the first class by writing "virtual":

virtual void mostrarDatos(char*, char*, char*);
virtual void calcularEdad(int);

So I can do the polymorphism, it affects the part of the code where suppose to do an addition:

s=student1+=student2;
t=student3+=student4;
u=s+=t;

if I do that, it shows some strange numbers instead of the right ones. Here is the complete code:

.h
#ifndef PERSON_H
#define PERSON_H
#include <iostream>
using namespace std;
class persona {

[Code] ......

View 2 Replies View Related

C++ :: Overload Virtual Member Function In Polymorphism?

Nov 30, 2013

I defined a virtual class and three other classes based on it. I want to use them like this:

int main() {
Dirichlet_t D;
Neumann_t N;
Cauchy_t C;

PDEBoundary_t * B1=& D;
PDEBoundary_t * B2=& N;
PDEBoundary_t * B3=& C;

[Code] .....

but I got two major errors
1: "object f abstract type is not allowed" error.-----why not?
2: "the derived class must implement the inherited pure virtual method"-----Did't I?

View 15 Replies View Related

C++ :: Shape Class Polymorphism Compiler Error

Dec 10, 2014

Having issues with program to create a shape area calculator with circle square and rectangle. the uml goes as follows:

Where the UML has shape as the abstract class with public area():double, getName():string,and getDimensions:string, rectangle derived from shape with protected height, and width, and a public rectangle(h:double, w:double), followed by a derived square from rectangle with just a public square(h:double), and finally a circle derived from shape with a private radius, and a public circle(r:double).

[URL]

Have linked my program and it is giving me the following compiler errors:

error: 'qdebug' was not declared in this scope line 15 of main

error: cannot declare variable 'shp' to be of abstract type 'shape' line 22 of main

error: expected primary-expression before ')' token lines 29 -31 of main

(note previously had qstring as a header file yet changed to string since I was getting error qstring was not declared in this scope.)

View 5 Replies View Related

C/C++ :: Polymorphism Is Stopping Short Of Desired Class

Mar 2, 2014

I've been working on this project which inserts data for different types of books lately, and I'm trying to utilize inheritance and polymorphism. The issue I've been having is after I create an object with my MediaFactory, I go to insert data into that type of object, but it won't reach the correct child class. The parent class in this case is MediaData. The class I'm trying to reach is Childrens, and the class that falls in between is called Book. The function I'm calling to insert the information is setData(ifstream&). This function simply places the information from a txt file into an object with the insertion operator.

Right now the program runs into the setData function of Book instead of Childrens. I need Childrens so I can enter all the required attributes (title,author,year). The call to this function is in MediaManager through the function called buildMedia, and my test is running into the case if (type == 'Y'). From there a pointer of type MediaData is created and pointed to a new object returned of type Childrens. I'm using my debugger in Xcode which does show that the correct object type (Childrens) was created.

I've tried a couple things so far. First I created another function called getData. From there I passed in *media and infile (txt input file) into that function, which then passed it right into setData with a pointer to the object and infile in the parameter. That didn't work, so I also tried going back into Childrens and removing MediaData from all my virtual functions and replacing it with Book. I got the same result when that happened; It morphed into Book class instead.

I have a portion of my UML as a reference. Childrens is a Book; Book is a MediaData. I also included all code for MediaManager, MediaHash, MediaFactory, MediaData, Book, and Childrens.

I did not include the operator overloads in the .cpp files to eliminate some redundancy.

//-----------------------------------------------------------------------------
// MediaManager.h
// Manager class for MediaData type objects
//-----------------------------------------------------------------------------

#ifndef MediaManager_H
#define MediaManager_H
#include "MediaHash.h"
#include "MediaFactory.h"
#include "MediaData.h"
using namespace std;
class MediaManager {

[Code] ....

View 3 Replies View Related

C++ :: Polymorphism On Class Instance Is Not Directly Applicable To Array

Mar 19, 2013

Below code produces run-time segmentation fault (core dumped) when execution reached line 53: delete [] array

Code 1:

#include <cstdlib>
#include <iostream>
using namespace std;
#define QUANTITY 5
class Parent {
protected:
int ID;

[Code] ....

Output of code 1:

Constructor of A: Instance created with ID=1804289383
Constructor of A: Instance created with ID=846930886
Constructor of A: Instance created with ID=1681692777
Constructor of A: Instance created with ID=1714636915
Constructor of A: Instance created with ID=1957747793
A::showID() -- ID is 1804289383
A::showID() -- ID is 846930886
A::showID() -- ID is 1681692777
A::showID() -- ID is 1714636915
A::showID() -- ID is 1957747793
Try to delete [] array..
Segmentation fault (core dumped)

Question: Why does segmentation fault happen in code 1 above?

View 9 Replies View Related

C++ ::  basic Polymorphism - Parent / Child Class Based Program

Oct 19, 2014

I am making a very basic parent/child class based program that shows polymorphism. It does not compile due to a few syntax errors reading "function call missing argument list. Lines 76 and 77, 81 and 82, and 86 and 87.

#include<iostream>
using namespace std;
class people {
public:
virtual void height(double h) = 0;
virtual void weight(double w) = 0;

[Code] ....

View 4 Replies View Related

C++ :: Inheritance Slows Down Process

Sep 5, 2013

i have read that inheritance slows down the process.because a_class -> b_class -> c_class it goes thorough many classes to find a function or a value. people advices not to do it more than a few times. what if instead of inheritance we are using headers, would it slow down the process also??i mean which one is faster? and arent both the same when we use headers or used inheritance.

View 3 Replies View Related

C++ :: Virtual Keyword In Inheritance?

Feb 19, 2014

How to put virtual keyword in the function of the base class. Will the function still be over-written properly? What will happen if I

(1) call function from base class pointer
(2) call function from derived class pointer
(3) call function from derived class object

If the virtual keyword is present, the over-written version will be called in all cases, am I mistaken?

View 1 Replies View Related

C++ ::  Inheritance Using Base Method With Other Name?

Feb 18, 2015

I have 2 classes with a Function with the same definition (both inherited from the same base class) and in my derived class I derive from both of those 2. Is it possible to use the Methods of both classes? for example with an other name?

class A {
protected:
int print(int a) { std::cout << "int A: " << a << std::endl; }
};
class B : A

[Code] ....

is there something like using C::print as printc;?

The Problem, I have a Sprite class that derives from a Rectangle with properties Position, Size, Origin and Angle and a Text class that derives from Rectangle. Now i have a Button class deriving from both Sprite and Text.

- The Position, when moving the Button i have to change the position of both so i Implemented a new Method which calls SetPosition from the Sprite and the Text.
- The SetSize just affects the Button so i just did using Sprite::SetSize;
- The angle affects both so i just implemented a new Method and hide the other two

The problem is here:
- The Origin: writing button.SetOrigin(-1,0) should set the Origin of the Button and writing button.SetTextOrigin should set the Origin of the text.

Should i just reimplement a Mehtod named SetTextOrigin and call Text::SetOrigin from there and hide the button.Text::SetOrigin or is there something like using Text::SetOrigin as SetTextOrigin?

View 6 Replies View Related

C++ :: Copy Constructors In Inheritance

May 16, 2013

#include <iostream>
using std::cout;
using std::endl;
class CBox // Base class definition
{
public:
// Base class constructor
explicit CBox(double lv = 1.0, double wv = 1.0, double hv = 1.0) : m_Length(lv), m_Width(wv), m_Height(hv)

[Code] .....

This example produces the following output:

// Derived class copy constructor
CCandyBox(const CCandyBox& initCB): CBox(initCB) {
std::cout << std::endl << "CCandyBox copy constructor called";
// Get new memory
m_Contents = new char[ strlen(initCB.m_Contents) + 1 ];
// Copy string
strcpy_s(m_Contents, strlen(initCB.m_Contents) + 1, initCB.m_Contents);
}

It will work right? Cause when I do "CBox(initCB)" it only sends the BASE part of the derived object to the base class copy constructor, is it copy or default?

View 6 Replies View Related

C++ :: Array Of Objects And Inheritance

Dec 20, 2013

I have Class A as a base class , and Class B , C derived classes from A and there's class D who have a data member (pointer to Array) of type A (Composition)

class D{
A **a;
int size;
.......
a = new A*[size];
......
};

And i have Print method , in its body i have to specific element (if it from class B or C ) with a given ID(both B and C have a data member ID ) there should be 2 options in print function .. printing elements for class B , or printing elements for class C ? how can i specific the elements ?

View 5 Replies View Related

C# :: Inheritance In WinForm Design

Feb 23, 2014

I have two Form Employee and Instructor, here Employee is super class of instructor.

public class Instructor: Employee
{
//Instructor class
}

I am able to access all property of Employee inside instructor class but with these some textbox and button design inside Employee design is also getting inherited inside Instructor design and i don want this and i want to maintain the parent-child relationship between Employee and Instructor.

View 11 Replies View Related

C++ :: Self-Deleting Objects With Inheritance

May 27, 2013

I'm writing a bunch of classes like Form, Button, etc. All these are derived from a base Object class. I want to make these objects auto-delete, so I only have to declare the new keyword, and the program will clean them when necessary. I understand the implications of dereferencing, I just have a couple of questions about my code.

I through this run-down example together:

Code:
#include <Windows.h>
#include <map>
class Object {
public:
Object() {};
~Object() {};
void *operator new(size_t size);
void operator delete(void *ptr);

[Code] .....

I have added a static variable of std::map type called m_Dynamic. I have overloaded the new and delete keywords (within the Object class) to keep the m_Dynamic map up-to-date. This prevents objects created on the stack from being deleted.

The object itself can then be deleted via the base Dispose() method. Or methods such as Destroy() can be added in derived classes and call upon Object::Dispose() themselves. This can be overloaded, etc, but eventually will be removed from the public view. I only have it here for testing.

From what I can tell everything "works", though I'm uncertain that the object is being correctly deleted.

Though, my main concern is that when a derived class such as Circle calls Dispose() which in turns fires delete this. Does it free() the sizeof(Object) or does it correctly free() the sizeof(Circle)?

Are there any other things I should be vary of? I only just started playing with new/delete overloading yesterday.

View 14 Replies View Related

C++ :: Array Of Class With Inheritance?

Jun 6, 2012

Shape base class, line and Point derived classes. What should I declare in .h files and implement in .cpp files that this is array will be work.

My major concern refer to operator [] and assign (=) operator. As far as I understand I should overload ([]) and (=) three times for classes shape , line and point or not... or is it possible made through virtual function? How will be code looks like ?

Code:
// part of main.cpp
Shape* shapes[3]; // Array of pointers to Shape
shapes[0] = new Shape();
shapes[1] = new Line ("line from array ", Point(1,22),Point(33,22));
shapes[2] = new Point(11,44);
cout << "using ToString function" << endl;
for(int i=0; i < 3; i++)
cout << s[i]->ToString();
for(i=0; i < 3; i++)
delete s[i];

View 2 Replies View Related

C++ :: Some Error With Inheritance Access Specifiers

Aug 5, 2013

let me show you the (simple) code:

Code:
#ifndef DUCK_H_
#define DUCK_H_
#include<iostream>
using namespace std;
class Duck

[Code].....

When I compile, it says: "error: 'virtual void Duck::display()' is protected" how come I can't gain access to MallardDuck's display(), which is public?

View 3 Replies View Related

C++ :: Trying To Implement Linked List Using Inheritance

Jan 5, 2014

Not inheriting properly and provide the corrected code if possible

Code:
#include <iostream>
using namespace std;
class Node {
protected:
int Data;

[Code] ....

View 4 Replies View Related

C++ :: Making A Money Bag - Class Inheritance

Feb 18, 2015

I am having some serious issues with class inheritance. I am trying to make a MoneyBag class inherit from a class called bag. This will not work. I get an error complaining: error: expected class-name before '{' token. And yes I have googleing it and tried several of the various solutions offered with no avail.

The MoneyBag is pretty simple right now as I wanted to get it connected to bag before I tried to do anything with it.

//MoneyBag.h//

#ifndef MONEYBAG_H
#define MONEYBAG_H
#include <bag.h>
class MoneyBag : public bag{ ////<<------ Error appears on this line.

[Code] ....

So based on everything I have seen on line the statement:
class MoneyBag : public bag{ is legal.
As it is done this way on this very site's tutorial:
class Rectangle: public Shape, public PaintCost{

View 2 Replies View Related

C++ :: Inheritance Info Lost In Container?

Sep 16, 2013

Code mostly speaks for itself:

#include <list>
#include <iostream>
using namespace std;

[Code]....

I added an object of class myderiv to the container, but when I retrieve it back and try to manipulate it, it's actually of class mybase. Is there any way to keep the inheritance information in the container?

View 8 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved